From cc78160e6eabd9f88b9042de655c583c94e6922e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 17 Jan 2023 10:08:00 +0100 Subject: [PATCH] add treefmt-nix --- flake.lock | 25 +++++++++++--- flake.nix | 29 +++++++++++----- nix/checks/flake-module.nix | 68 +++++++++++++++++++++++++++++++++++++ nix/checks/treefmt.nix | 22 ------------ shell.nix | 5 --- treefmt.toml | 21 ------------ 6 files changed, 109 insertions(+), 61 deletions(-) create mode 100644 nix/checks/flake-module.nix delete mode 100644 nix/checks/treefmt.nix delete mode 100644 treefmt.toml diff --git a/flake.lock b/flake.lock index 70c0ab2..dd7e993 100644 --- a/flake.lock +++ b/flake.lock @@ -22,15 +22,16 @@ }, "nixpkgs": { "locked": { - "lastModified": 1673944933, - "narHash": "sha256-yrWHhOj7BwY/dbQ+Ifhanm/IZ6qWerZiGmFbmt0/wMU=", + "lastModified": 1673606088, + "narHash": "sha256-wdYD41UwNwPhTdMaG0AIe7fE1bAdyHe6bB4HLUqUvck=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "81aef2338a3834f4d0071978dbc114497ec56a7e", + "rev": "37b97ae3dd714de9a17923d004a2c5b5543dfa6d", "type": "github" }, "original": { "owner": "NixOS", + "ref": "nixpkgs-unstable", "repo": "nixpkgs", "type": "github" } @@ -38,7 +39,23 @@ "root": { "inputs": { "flake-parts": "flake-parts", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "treefmt-nix": "treefmt-nix" + } + }, + "treefmt-nix": { + "locked": { + "lastModified": 1672931382, + "narHash": "sha256-lgtc2Sct/xtvqkdzlJ4AL3Vesw0Wz/fxqNGOBFS7YXU=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "6717065d6a432bc3f5b827007ad959e6032d5856", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" } } }, diff --git a/flake.nix b/flake.nix index ca80b8b..d74c0ac 100644 --- a/flake.nix +++ b/flake.nix @@ -4,12 +4,17 @@ inputs = { flake-parts.url = "github:hercules-ci/flake-parts"; flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; - nixpkgs.url = "github:NixOS/nixpkgs"; + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + + treefmt-nix.url = "github:numtide/treefmt-nix"; }; outputs = inputs @ {flake-parts, ...}: flake-parts.lib.mkFlake {inherit inputs;} { systems = ["x86_64-linux" "aarch64-linux"]; + imports = [ + ./nix/checks/flake-module.nix + ]; perSystem = { config, self', @@ -19,15 +24,21 @@ ... }: { packages.default = pkgs.callPackage ./default.nix {}; - devShells.default = pkgs.callPackage ./shell.nix {}; - checks = let - nixosTests = pkgs.callPackages ./nix/checks/nixos-test.nix { - makeTest = import (pkgs.path + "/nixos/tests/make-test-python.nix"); - }; - in { - treefmt = pkgs.callPackage ./nix/checks/treefmt.nix {}; - inherit (nixosTests) unittests vault-agent systemd-vaultd; + devShells.default = pkgs.mkShellNoCC { + buildInputs = with pkgs; [ + python3.pkgs.pytest + python3.pkgs.mypy + + golangci-lint + vault + systemd + hivemind + go + just + config.packages.treefmt + ]; }; + }; flake.nixosModules = { vaultAgent = ./nix/modules/vault-agent.nix; diff --git a/nix/checks/flake-module.nix b/nix/checks/flake-module.nix new file mode 100644 index 0000000..14235f5 --- /dev/null +++ b/nix/checks/flake-module.nix @@ -0,0 +1,68 @@ +{inputs, ...}: { + imports = [ + inputs.treefmt-nix.flakeModule + ]; + perSystem = { + self', + inputs', + pkgs, + system, + ... + }: { + treefmt = { + # Used to find the project root + projectRootFile = "flake.lock"; + + programs.gofumpt.enable = true; + programs.prettier.enable = true; + + settings.formatter = { + nix = { + command = "sh"; + options = [ + "-eucx" + '' + # First deadnix + ${pkgs.lib.getExe pkgs.deadnix} --edit "$@" + # Then nixpkgs-fmt + ${pkgs.lib.getExe pkgs.nixpkgs-fmt} "$@" + '' + "--" + ]; + includes = ["*.nix"]; + }; + + python = { + command = "sh"; + options = [ + "-eucx" + '' + ${pkgs.lib.getExe pkgs.ruff} --fix "$@" + ${pkgs.lib.getExe pkgs.python3.pkgs.black} "$@" + '' + "--" # this argument is ignored by bash + ]; + includes = ["*.py"]; + }; + }; + + checks = + let + nixosTests = pkgs.callPackages ./nixos-test.nix { + makeTest = import (pkgs.path + "/nixos/tests/make-test-python.nix"); + }; + in + { + inherit (nixosTests) unittests vault-agent systemd-vaultd; + }; + }; + + checks = let + nixosTests = pkgs.callPackages ./nix/checks/nixos-test.nix { + makeTest = import (pkgs.path + "/nixos/tests/make-test-python.nix"); + }; + in { + inherit (nixosTests) unittests vault-agent systemd-vaultd; + }; + }; +} diff --git a/nix/checks/treefmt.nix b/nix/checks/treefmt.nix deleted file mode 100644 index b3da705..0000000 --- a/nix/checks/treefmt.nix +++ /dev/null @@ -1,22 +0,0 @@ -{ - runCommand, - gofumpt, - alejandra, - python3, - treefmt, -}: -runCommand "treefmt" { - nativeBuildInputs = [ - gofumpt - treefmt - alejandra - python3.pkgs.flake8 - python3.pkgs.black - ]; -} '' - # keep timestamps so that treefmt is able to detect mtime changes - cp --no-preserve=mode --preserve=timestamps -r ${../..} source - cd source - HOME=$TMPDIR treefmt --no-cache --fail-on-change - touch $out -'' diff --git a/shell.nix b/shell.nix index cd5fc83..a9bb039 100644 --- a/shell.nix +++ b/shell.nix @@ -3,18 +3,13 @@ with pkgs; mkShellNoCC { buildInputs = [ python3.pkgs.pytest - python3.pkgs.flake8 - python3.pkgs.black python3.pkgs.mypy - gofumpt golangci-lint - alejandra vault systemd hivemind go - treefmt just ]; } diff --git a/treefmt.toml b/treefmt.toml deleted file mode 100644 index 3aaedae..0000000 --- a/treefmt.toml +++ /dev/null @@ -1,21 +0,0 @@ -[formatter.nix] -command = "alejandra" -includes = ["*.nix"] - -[formatter.python] -command = "sh" -options = [ - "-eucx", - """ -flake8 "$@" -black "$@" - """ -] -includes = ["*.py"] -excludes = [] - -[formatter.go] -command = "gofumpt" -options = ["-w"] -includes = ["*.go"] -excludes = []