{"id":13571192,"url":"https://github.com/NixOS/nixfmt","last_synced_at":"2025-04-04T07:33:14.581Z","repository":{"id":35088385,"uuid":"143292270","full_name":"NixOS/nixfmt","owner":"NixOS","description":"The official (but not yet stable) formatter for Nix code","archived":false,"fork":false,"pushed_at":"2025-03-18T16:20:42.000Z","size":1022,"stargazers_count":1070,"open_issues_count":27,"forks_count":48,"subscribers_count":35,"default_branch":"master","last_synced_at":"2025-03-28T08:52:28.452Z","etag":null,"topics":["formatter","haskell","nix"],"latest_commit_sha":null,"homepage":"https://nixfmt.serokell.io","language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NixOS.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"nixos"}},"created_at":"2018-08-02T12:33:51.000Z","updated_at":"2025-03-28T05:05:00.000Z","dependencies_parsed_at":"2023-10-11T17:49:05.327Z","dependency_job_id":"9cbecc06-6498-4eb8-b12c-68c449a4b6c6","html_url":"https://github.com/NixOS/nixfmt","commit_stats":{"total_commits":413,"total_committers":27,"mean_commits":"15.296296296296296","dds":0.6997578692493946,"last_synced_commit":"30907b667b682564b7e54061bc4e3446305bc168"},"previous_names":["nixos/nixfmt","serokell/nixfmt"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NixOS%2Fnixfmt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NixOS%2Fnixfmt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NixOS%2Fnixfmt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NixOS%2Fnixfmt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NixOS","download_url":"https://codeload.github.com/NixOS/nixfmt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246706674,"owners_count":20820785,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["formatter","haskell","nix"],"created_at":"2024-08-01T14:00:59.703Z","updated_at":"2025-04-04T07:33:14.573Z","avatar_url":"https://github.com/NixOS.png","language":"Haskell","funding_links":["https://opencollective.com/nixos"],"categories":["Command-Line Tools","Haskell","Recently Updated"],"sub_categories":["Discovery","[Who Wants to Be a Millionare](https://www.boardgamecapital.com/who-wants-to-be-a-millionaire-rules.htm)"],"readme":"# `nixfmt`\n\n`nixfmt` is a formatter for Nix code, intended to easily apply a uniform style.\n\nYou are encouraged to test this out on your code and submit any undesirable formatting you find as an issue\n\n![Build Status](https://github.com/NixOS/nixfmt/actions/workflows/main.yml/badge.svg?branch=master)\n\n## State\n\n`nixfmt` will form the basis for the initial official standard Nix formatter, as established by [RFC 166](https://github.com/NixOS/rfcs/pull/166).\n\nThe established standard Nix formatting differs considerably from the original one. Be aware of this if you track the main branch. Until the first new release the main branch should be considered **very unstable**.\n\nFor more details, see the [RFC implementation tracking issue](https://github.com/NixOS/nixfmt/issues/153).\n\n## Installation And Usage Instructions\n\n### nixpkgs/NixOS\n\n`nixfmt` was used as the basis for the official Nix formatter with a standardized formatting.\nThe new formatting differs considerably from the original one.\nA recent nixfmt version is available as `pkgs.nixfmt-rfc-style` in Nixpkgs.\nThe formatting of this version differs considerably from the original nixfmt that was used as the basis for the standardised official formatter, which is also still available as `pkgs.nixfmt-classic` for now, though unmaintained.\n\nSo installing this `nixfmt` is as simple as adding to the system packages:\n\n```nix\n{ pkgs, ... }:\n\n{\n  environment.systemPackages = [ pkgs.nixfmt-rfc-style ];\n}\n```\n\n### From the repository\n\nIt's also possible to install `nixfmt` directly from the repository using `nix-env`.\nAlso, updates are not done automatically (as it would with the system packages).\n\n```console\n$ nix-env -i -f https://github.com/NixOS/nixfmt/archive/master.tar.gz\n```\n\n### nix fmt\n\n[nix fmt](https://nix.dev/manual/nix/latest/command-ref/new-cli/nix3-fmt) (which is a flakes-only feature) can be configured by adding the following to `flake.nix` (assuming a `nixpkgs` input exists):\n```nix\n{\n  outputs =\n    { nixpkgs, self }:\n    {\n      formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixfmt-rfc-style;\n    };\n}\n```\n\n### treefmt\n\n[treefmt](https://github.com/numtide/treefmt) can be used to format repositories consisting of different languages with one command.\nA possible configuration for `nixfmt` in `treefmt.toml` looks like this:\n```toml\n[formatter.nixfmt-rfc-style]\ncommand = \"nixfmt\"\nincludes = [\"*.nix\"]\n```\n\nThis only works when `nixfmt-rfc-style` is installed (see above for installation instructions).\n\n`treefmt` can be integrated into text editors and CI to ensure consistent formatting for all filetypes.\n\n### treefmt-nix\n\n[treefmt-nix](https://github.com/numtide/treefmt-nix) automatically configures the correct packages and formatters using a Nix configuration and has native support for `nixfmt`:\n\n```nix\n# ...\ntreefmt-nix.mkWrapper nixpkgs {\n  # ... other options ...\n  programs.nixfmt-rfc-style.enable = true;\n}\n```\n\nMore information about configuration can be found in [the README](https://github.com/numtide/treefmt-nix?tab=readme-ov-file#integration-into-nix).\n\n### git-hooks.nix\n\n[git-hooks.nix](https://github.com/cachix/git-hooks.nix) can automatically configure git hooks like `pre-commit` using nix configuration and has native support for `nixfmt`:\n\n```nix\n{\n  pre-commit-check = nix-pre-commit-hooks.run {\n    # ... other options ...\n    hooks = {\n      # ... other hooks ...\n      nixfmt-rfc-style.enable = true;\n    };\n  };\n}\n```\n\n### `pre-commit` tool\n\nIf you have Nix files in a Git repo and you want to make sure that they’re formatted with `nixfmt`, then you can use the `pre-commit` tool from [pre-commit.com](https://pre-commit.com):\n\n1. Make sure that you have the `pre-commit` command:\n\n    ```console\n    $ pre-commit --version\n    pre-commit 3.7.1\n    ```\n\n2. Make sure that you’re in your Git repo:\n\n    ```console\n    $ cd \u003cpath-to-git-repo\u003e\n    ```\n\n3. Make sure that the `pre-commit` tool is installed as a Git pre-commit hook:\n\n    ```console\n    $ pre-commit install\n    pre-commit installed at .git/hooks/pre-commit\n    ```\n\n4. If you don’t already have one, then create a `.pre-commit-config.yaml` file.\n\n5. Add an entry for the `nixfmt` hook to your `.pre-commit-config.yaml` file:\n\n    ```yaml\n    repos:\n        - repo: https://github.com/NixOS/nixfmt\n          rev: \u003cversion\u003e\n          hooks:\n                - id: nixfmt\n    ```\n\n    If you want to use a stable version of `nixfmt`, then replace `\u003cversion\u003e` with a tag from this repo. If you want to use an unstable version of `nixfmt`, then replace `\u003cversion\u003e` with a commit hash from this repo.\n\n6. Try to commit a badly formatted Nix file in order to make sure that everything works.\n\n\u003e [!WARNING]\n\u003e `nixfmt`’s integration with the `pre-commit` tool is relatively new. At the moment, none of the stable releases of `nixfmt` can be used with the `pre-commit` tool. You’ll have to use an unstable version for the time being.\n\n### neovim + nixd\n\n```lua\nlocal nvim_lsp = require(\"lspconfig\")\nnvim_lsp.nixd.setup({\n   settings = {\n      nixd = {\n         formatting = {\n            command = { \"nixfmt\" },\n         },\n      },\n   },\n})\n```\n\nThis only works when `nixfmt-rfc-style` is installed (see above for installation instructions).\n\n### neovim + nil\n\n```lua\nlocal nvim_lsp = require(\"lspconfig\")\nnvim_lsp.nil_ls.setup({\n   settings = {\n      ['nil'] = {\n         formatting = {\n            command = { \"nixfmt\" },\n         },\n      },\n   },\n})\n```\n\nThis only works when `nixfmt-rfc-style` is installed (see above for installation instructions).\n\n### neovim + none-ls\n\n```lua\nlocal null_ls = require(\"null-ls\")\nnull_ls.setup({\n    sources = {\n        null_ls.builtins.formatting.nixfmt,\n    },\n})\n```\n\nThis only works when `nixfmt-rfc-style` is installed (see above for installation instructions).\n\n### git mergetool\n\nNixfmt provides a mode usable by [`git mergetool`](https://git-scm.com/docs/git-mergetool)\nvia `--mergetool` that allows resolving formatting-related conflicts automatically in many cases.\n\nIt can be installed by any of these methods:\n\n- For only for the current repo, run:\n  ```\n  git config mergetool.nixfmt.cmd 'nixfmt --mergetool \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"'\n  git config mergetool.nixfmt.trustExitCode true\n  ```\n- For all repos with a mutable config file, run\n  ```\n  git config --global mergetool.nixfmt.cmd 'nixfmt --mergetool \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"'\n  git config --global mergetool.nixfmt.trustExitCode true\n  ```\n- For all repos with a NixOS-provided config file, add this to your `configuration.nix`:\n  ```nix\n  programs.git.config = {\n    mergetool.nixfmt = {\n      cmd = \"nixfmt --mergetool \\\"$BASE\\\" \\\"$LOCAL\\\" \\\"$REMOTE\\\" \\\"$MERGED\\\"\";\n      trustExitCode = true;\n    };\n  };\n  ```\n- For all repos with a home-manager-provided config file, add this to your `home.nix`:\n  ```nix\n  programs.git.extraConfig = {\n    mergetool.nixfmt = {\n      cmd = \"nixfmt --mergetool \\\"$BASE\\\" \\\"$LOCAL\\\" \\\"$REMOTE\\\" \\\"$MERGED\\\"\";\n      trustExitCode = true;\n    };\n  };\n  ```\n\nThen, when `git merge` or `git rebase` fails, run\n```\ngit mergetool -t nixfmt .\n# or, only for some specific files\ngit mergetool -t nixfmt FILE1 FILE2 FILE3\n```\n\nand some `.nix` files will probably get merged automagically.\n\nNote that files that `git` merges successfully even before `git mergetool`\nwill be ignored by \\`git mergetool\\`.\n\nIf you don't like the result, run\n```\ngit restore --merge .\n# or, only for some specific files\ngit restore --merge FILE1 FILE2 FILE3\n```\n\nto return back to the unmerged state.\n\n## Development\n\n### With Nix\n\nHaskell dependencies will be built by Nix.\n\n* Enter `nix-shell`\n* Build with `cabal new-build`\n\n### Without Nix\n\nHaskell dependencies will be built by Cabal.\n\n* Build with `cabal new-build`\n\n\n## Usage\n\n* `nixfmt \u003c input.nix` – reads Nix code from `stdin`, formats it, and outputs to `stdout`\n* `nixfmt file.nix` – format the file in place\n\n## About Serokell\n\n`nixfmt` is maintained and funded with :heart: by\n[Serokell](https://serokell.io/). The names and logo for Serokell are trademark\nof Serokell OÜ.\n\nWe love open source software! See\n[our other projects](https://serokell.io/community?utm_source=github) or\n[hire us](https://serokell.io/hire-us?utm_source=github) to design, develop and\ngrow your idea!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNixOS%2Fnixfmt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNixOS%2Fnixfmt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNixOS%2Fnixfmt/lists"}