From 0d30f770a3448827d0f483eea622c1db825096b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Domen=20Ko=C5=BEar?= Date: Mon, 26 Apr 2021 17:58:34 +0200 Subject: [PATCH] flakes support --- .github/workflows/ci.yaml | 19 ++++++++++++++++++ flake.lock | 41 +++++++++++++++++++++++++++++++++++++++ flake.nix | 21 ++++++++++++++++++++ modules/pre-commit.nix | 13 +------------ nix/default.nix | 17 ++++++++-------- nix/run.nix | 14 ++++++++----- 6 files changed, 100 insertions(+), 25 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 77fcbe98..404fcb37 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -16,3 +16,22 @@ jobs: name: pre-commit-hooks signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}' - run: nix-build + tests-flakes: + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v2 + - uses: cachix/install-nix-action@v13 + with: + install_url: https://nixos-nix-install-tests.cachix.org/serve/lb41az54kzk6j12p81br4bczary7m145/install + install_options: '--tarball-url-prefix https://nixos-nix-install-tests.cachix.org/serve' + extra_nix_config: | + experimental-features = nix-command flakes + - uses: cachix/cachix-action@v10 + with: + name: pre-commit-hooks + signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}' + - run: nix flake check + - run: nix eval .#lib.x86_64-linux.run \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..a923baa0 --- /dev/null +++ b/flake.lock @@ -0,0 +1,41 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1619345332, + "narHash": "sha256-qHnQkEp1uklKTpx3MvKtY6xzgcqXDsz5nLilbbuL+3A=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "2ebf2558e5bf978c7fb8ea927dfaed8fefab2e28", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1619531122, + "narHash": "sha256-ovm5bo6PkZzNKh2YGXbRKYIjega0EjiEP0YDwyeXEYU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "bb80d578e8ad3cb5a607f468ac00cc546d0396dc", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..1557225d --- /dev/null +++ b/flake.nix @@ -0,0 +1,21 @@ +{ + description = "Seamless integration of https://pre-commit.com git hooks with Nix."; + + inputs.flake-utils.url = "github:numtide/flake-utils"; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + exposed = import ./nix { nixpkgs = nixpkgs; inherit system; gitignore-nix-src = null; isFlakes = true; }; + in + { + packages = exposed.packages; + + defaultPackage = exposed.packages.pre-commit; + + checks = exposed.checks; + + lib = { inherit (exposed) run; }; + } + ); +} diff --git a/modules/pre-commit.nix b/modules/pre-commit.nix index c404618c..4222168e 100644 --- a/modules/pre-commit.nix +++ b/modules/pre-commit.nix @@ -163,11 +163,6 @@ let touch $out [ $? -eq 0 ] && exit $exitcode ''; - - # TODO: provide a default pin that the user may override - inherit (import (import ../nix/sources.nix)."gitignore.nix" { inherit lib; }) - gitignoreSource - ; in { options = @@ -180,7 +175,6 @@ in '' The pre-commit package to use. ''; - default = pkgs.pre-commit; defaultText = literalExample '' pkgs.pre-commit @@ -190,16 +184,12 @@ in tools = mkOption { type = types.lazyAttrsOf types.package; - description = '' Tool set from which nix-pre-commit will pick binaries. nix-pre-commit comes with its own set of packages for this purpose. ''; - # This default is for when the module is the entry point rather than - # /default.nix. /default.nix will override this for efficiency. - default = (import ../nix { inherit (pkgs) system; }).callPackage ../nix/tools.nix { }; defaultText = literalExample ''nix-pre-commit-hooks-pkgs.callPackage tools-dot-nix { inherit (pkgs) system; }''; }; @@ -247,7 +237,7 @@ in rootSrc = mkOption { - type = types.package; + type = types.path; description = '' The source of the project to be checked. @@ -255,7 +245,6 @@ in This is used in the derivation that performs the check. ''; defaultText = literalExample ''gitignoreSource config.src''; - default = gitignoreSource config.src; }; excludes = diff --git a/nix/default.nix b/nix/default.nix index e76e9b57..2d7a0d17 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -1,25 +1,26 @@ { sources ? import ./sources.nix , system ? builtins.currentSystem , nixpkgs ? sources.nixpkgs +, gitignore-nix-src ? sources."gitignore.nix" +, isFlakes ? false }: let overlay = - _: pkgs: + self: pkgs: let - run = pkgs.callPackage ./run.nix { inherit tools; }; - tools = pkgs.callPackage ./tools.nix { }; + tools = pkgs.lib.filterAttrs (k: v: !(pkgs.lib.any (a: k == a) [ "override" "overrideDerivation" ])) (pkgs.callPackage ./tools.nix { }); + run = pkgs.callPackage ./run.nix { inherit pkgs tools isFlakes gitignore-nix-src; }; in { inherit (pkgs) nixfmt niv ormolu nixpkgs-fmt nix-linter; cabal-fmt = pkgs.haskell.lib.enableSeparateBinOutput pkgs.haskellPackages.cabal-fmt; hindent = pkgs.haskell.lib.enableSeparateBinOutput pkgs.haskellPackages.hindent; - inherit tools; + inherit tools run; # Flake style attributes - packages = { - inherit tools run; - inherit (pkgs.gitAndTools) pre-commit; + packages = tools // { + inherit (pkgs) pre-commit; }; - checks = tools // { + checks = self.packages // { # A pre-commit-check for nix-pre-commit itself pre-commit-check = run { src = ../.; diff --git a/nix/run.nix b/nix/run.nix index af6731da..c1f7af55 100644 --- a/nix/run.nix +++ b/nix/run.nix @@ -1,4 +1,4 @@ -builtinStuff@{ pkgs, tools, pre-commit, git, runCommand, writeText, writeScript, lib }: +builtinStuff@{ pkgs, tools, isFlakes, pre-commit, git, runCommand, writeText, writeScript, lib, gitignore-nix-src }: { src , hooks ? { } @@ -8,8 +8,6 @@ builtinStuff@{ pkgs, tools, pre-commit, git, runCommand, writeText, writeScript, , default_stages ? [ ] }: let - sources = import ./sources.nix; - project = lib.evalModules { modules = @@ -19,9 +17,15 @@ let config = { _module.args.pkgs = pkgs; - inherit hooks excludes settings src default_stages; + _module.args.gitignore-nix-src = gitignore-nix-src; + inherit hooks excludes settings default_stages; tools = builtinStuff.tools // tools; - }; + package = pre-commit; + } // (if isFlakes + then { rootSrc = src; } + else { + rootSrc = (import gitignore-nix-src { inherit (pkgs) lib; }).gitignoreSource src; + }); } ]; };