diff --git a/flake.lock b/flake.lock index 26a60e4..9200042 100644 --- a/flake.lock +++ b/flake.lock @@ -2071,6 +2071,7 @@ "nixvim": "nixvim", "pre-commit-hooks": "pre-commit-hooks_6", "stylix": "stylix", + "treefmt-nix": "treefmt-nix_5", "whisper-overlay": "whisper-overlay", "wired-notify": "wired-notify" } @@ -2511,6 +2512,26 @@ "type": "github" } }, + "treefmt-nix_5": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1732292307, + "narHash": "sha256-5WSng844vXt8uytT5djmqBCkopyle6ciFgteuA9bJpw=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "705df92694af7093dfbb27109ce16d828a79155f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + }, "treefmt_2": { "inputs": { "nixpkgs": [ diff --git a/flake.nix b/flake.nix index 220ee96..28c9c74 100644 --- a/flake.nix +++ b/flake.nix @@ -95,6 +95,11 @@ inputs.home-manager.follows = "home-manager"; }; + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + whisper-overlay = { url = "github:oddlama/whisper-overlay"; inputs.nixpkgs.follows = "nixpkgs"; @@ -106,8 +111,9 @@ }; }; - outputs = inputs: - inputs.flake-parts.lib.mkFlake {inherit inputs;} { + outputs = + inputs: + inputs.flake-parts.lib.mkFlake { inherit inputs; } { imports = [ ./nix/agenix-rekey.nix ./nix/devshell.nix diff --git a/nix/devshell.nix b/nix/devshell.nix index eb0d54f..51e1548 100644 --- a/nix/devshell.nix +++ b/nix/devshell.nix @@ -1,99 +1,93 @@ -{inputs, ...}: { +{ inputs, ... }: +{ imports = [ inputs.devshell.flakeModule inputs.pre-commit-hooks.flakeModule + inputs.treefmt-nix.flakeModule ]; - perSystem = { - config, - pkgs, - ... - }: { - pre-commit.settings.hooks = { - alejandra.enable = true; - deadnix.enable = true; - statix.enable = true; - #gitleaks = { - # enable = true; - # name = "gitleaks"; - # entry = "${pkgs.gitleaks}/bin/gitleaks protect --verbose --redact --staged"; - # language = "system"; - # pass_filenames = false; - #}; - }; + perSystem = + { + config, + pkgs, + ... + }: + { + pre-commit.settings.hooks.treefmt.enable = true; + treefmt = { + projectRootFile = "flake.nix"; + programs = { + deadnix.enable = true; + statix.enable = true; + nixfmt.enable = true; + rustfmt.enable = true; + }; + }; - devshells.default = { - packages = [ - pkgs.nix # Always use the nix version from this flake's nixpkgs version, so that nix-plugins (below) doesn't fail because of different nix versions. - ]; + devshells.default = { + packages = [ + pkgs.nix # Always use the nix version from this flake's nixpkgs version, so that nix-plugins (below) doesn't fail because of different nix versions. + ]; - commands = [ - { - package = pkgs.deploy; - help = "Build and deploy this nix config to nodes"; - } - { - package = pkgs.alejandra; - help = "Format nix code"; - } - { - package = pkgs.statix; - help = "Lint nix code"; - } - { - package = pkgs.deadnix; - help = "Find unused expressions in nix code"; - } - { - package = pkgs.nix-tree; - help = "Interactively browse dependency graphs of Nix derivations"; - } - { - package = pkgs.nvd; - help = "Diff two nix toplevels and show which packages were upgraded"; - } - { - package = pkgs.nix-diff; - help = "Explain why two Nix derivations differ"; - } - { - package = pkgs.nix-output-monitor; - help = "Nix Output Monitor (a drop-in alternative for `nix` which shows a build graph)"; - } - { - package = pkgs.writeShellApplication { - name = "build"; - text = '' - set -euo pipefail - [[ "$#" -ge 1 ]] \ - || { echo "usage: build ..." >&2; exit 1; } - HOSTS=() - for h in "$@"; do - HOSTS+=(".#nixosConfigurations.$h.config.system.build.toplevel") - done - nom build --no-link --print-out-paths --show-trace "''${HOSTS[@]}" + commands = [ + { + package = config.treefmt.build.wrapper; + help = "Format all files"; + } + { + package = pkgs.deploy; + help = "Build and deploy this nix config to nodes"; + } + { + package = pkgs.nix-tree; + help = "Interactively browse dependency graphs of Nix derivations"; + } + { + package = pkgs.nvd; + help = "Diff two nix toplevels and show which packages were upgraded"; + } + { + package = pkgs.nix-diff; + help = "Explain why two Nix derivations differ"; + } + { + package = pkgs.nix-output-monitor; + help = "Nix Output Monitor (a drop-in alternative for `nix` which shows a build graph)"; + } + { + package = pkgs.writeShellApplication { + name = "build"; + text = '' + set -euo pipefail + [[ "$#" -ge 1 ]] \ + || { echo "usage: build ..." >&2; exit 1; } + HOSTS=() + for h in "$@"; do + HOSTS+=(".#nixosConfigurations.$h.config.system.build.toplevel") + done + nom build --no-link --print-out-paths --show-trace "''${HOSTS[@]}" + ''; + }; + help = "Build a host configuration"; + } + ]; + + devshell.startup.pre-commit.text = config.pre-commit.installationScript; + + env = [ + { + # Additionally configure nix-plugins with our extra builtins file. + # We need this for our repo secrets. + name = "NIX_CONFIG"; + value = '' + plugin-files = ${pkgs.nix-plugins}/lib/nix/plugins + extra-builtins-file = ${./..}/nix/extra-builtins.nix ''; - }; - help = "Build a host configuration"; - } - ]; + } + ]; + }; - devshell.startup.pre-commit.text = config.pre-commit.installationScript; - - env = [ - { - # Additionally configure nix-plugins with our extra builtins file. - # We need this for our repo secrets. - name = "NIX_CONFIG"; - value = '' - plugin-files = ${pkgs.nix-plugins}/lib/nix/plugins - extra-builtins-file = ${./..}/nix/extra-builtins.nix - ''; - } - ]; + # `nix fmt` + formatter = pkgs.alejandra; }; - - # `nix fmt` - formatter = pkgs.alejandra; - }; }