diff --git a/crate2nix/default.nix b/crate2nix/default.nix index 5a850653..78fa1dec 100644 --- a/crate2nix/default.nix +++ b/crate2nix/default.nix @@ -1,9 +1,6 @@ # Provided by callPackage or also directly usable via nix-build with defaults. { pkgs ? ( - let - flakeLock = builtins.fromJSON (builtins.readFile ../flake.lock); - in - import "${builtins.fetchTree flakeLock.nodes.nixpkgs.locked}" { } + import (builtins.fetchTree (import ../nix/flakeInput.nix "nixpkgs")) { } ) , stdenv ? pkgs.stdenv , lib ? pkgs.lib diff --git a/docs/src/content/docs/00_guides/70_building_fetched_sources.md b/docs/src/content/docs/00_guides/70_building_fetched_sources.md index 74a8d87d..629d737e 100644 --- a/docs/src/content/docs/00_guides/70_building_fetched_sources.md +++ b/docs/src/content/docs/00_guides/70_building_fetched_sources.md @@ -17,14 +17,21 @@ using the [tools.nix](./31_auto_generating) support: # nix/nix-test-runner.nix let # Reuses the locked flake inputs. - flakeLock = builtins.fromJSON (builtins.readFile ../flake.lock); + flakeInput = import ./flakeInput.nix; # Gets the locked sources. - src = builtins.fetchTree flakeLock.nodes.nix-test-runner.locked; + src = builtins.fetchTree (flakeInput "nix-test-runner"); + + # Use last pinned crate2nix packages and corresponding nixpkgs to build the + # test runner so that it works even if we have broken stuff! + crate2nix_stable = builtins.fetchTree (flakeInput "crate2nix_stable"); + nixpkgs_stable = builtins.fetchTree (flakeInput "crate2nix_stable.nixpkgs"); in -{ pkgs ? import ./nixpkgs.nix { } - # Use last pinned crate2nix packages to build the test runner - # so that it works even if we have broken stuff! -, tools ? pkgs.callPackage "${builtins.fetchTree flakeLock.nodes.crate2nix_stable.locked}/tools.nix" { } +{ + # A system must be specified if using default value for pkgs and calling this + # package from a pure evaluation context, such as from the flake devShell. + system ? builtins.currentSystem +, pkgs ? import nixpkgs_stable { inherit system; } +, tools ? pkgs.callPackage "${crate2nix_stable}/tools.nix" { } }: let nixTestRunner = tools.appliedCargoNix { @@ -38,9 +45,9 @@ nixTestRunner.rootCrate.build ```nix # nix/nixpkgs.nix let - flakeLock = builtins.fromJSON (builtins.readFile ../flake.lock); + flakeInput = import ./flakeInput.nix; in -import "${builtins.fetchTree flakeLock.nodes.nixpkgs.locked}" +import (builtins.fetchTree (flakeInput "nixpkgs")) ``` ```nix diff --git a/nix/devshell/flake-module.nix b/nix/devshell/flake-module.nix index e39497f6..87a5b3e3 100644 --- a/nix/devshell/flake-module.nix +++ b/nix/devshell/flake-module.nix @@ -32,7 +32,7 @@ { package = nix-prefetch-git; category = "nix"; } { name = "nix-test"; - package = (import ../nix-test-runner.nix { inherit pkgs; }); + package = (import ../nix-test-runner.nix { inherit (pkgs) system; }); category = "nix"; help = "nix test runner for unit tests."; } diff --git a/nix/flakeInput.nix b/nix/flakeInput.nix new file mode 100644 index 00000000..03c47e57 --- /dev/null +++ b/nix/flakeInput.nix @@ -0,0 +1,30 @@ +# Get a locked flake input value that can be given to `builtins.fetchTree` or to +# `builtins.getFlake`. For example, +# +# pkgs = import (builtins.fetchTree (flakeInput "nixpkgs")) { } +# +# This function can also be used to get inputs of inputs using dot-separated +# paths. For example, +# +# pkgs = import (builtins.fetchTree (flakeInput "crate2nix_stable.nixpkgs")) { } +# +# Gets the nixpkgs input of the crate2nix_stable input. + +let + flakeLock = builtins.fromJSON (builtins.readFile ../flake.lock); + flakeInputNodeOf = parentNode: inputName: + let + inputNodeName = builtins.getAttr inputName parentNode.inputs; + in + builtins.getAttr inputNodeName flakeLock.nodes; + rootNode = let rootName = flakeLock.root; in builtins.getAttr rootName flakeLock.nodes; +in + +name: +let + parts = builtins.split "[.]" name; + inputNames = builtins.filter builtins.isString parts; + flakeNode = builtins.foldl' flakeInputNodeOf rootNode inputNames; +in +flakeNode.locked + diff --git a/nix/nix-test-runner.nix b/nix/nix-test-runner.nix index a366b620..02aeb0d5 100644 --- a/nix/nix-test-runner.nix +++ b/nix/nix-test-runner.nix @@ -1,11 +1,36 @@ let - flakeLock = builtins.fromJSON (builtins.readFile ../flake.lock); - src = builtins.fetchTree flakeLock.nodes.nix-test-runner.locked; + flakeInput = import ./flakeInput.nix; + src = builtins.fetchTree (flakeInput "nix-test-runner"); + + # Use last pinned crate2nix packages and corresponding nixpkgs to build the + # test runner so that it works even if we have broken stuff! + crate2nix_stable = builtins.fetchTree (flakeInput "crate2nix_stable"); + + # For consistency we should use the locked version of nixpkgs from the + # crate2nix_stable release which would be, + # + # nixpkgs_stable = builtins.fetchTree (flakeInput "crate2nix_stable.nixpkgs"); + # + # But nix-test-runner fails to build with the crate2nix_stable and the pinned + # nixpkgs from the same release. That's because "crate2nix_stable.nixpkgs" + # provides cargo v1.78 while "nixpkgs" provides cargo v1.78. Between those two + # cargo versions `cargo metadata` changed the format it uses for package ID + # strings which breaks checksum lookups in legacy V1 Cargo.toml files in + # crate2nix - and it happens that nix-test-runner uses a V1 Cargo.toml + # manifest. + # + # So for the time being it is necessary to use the current "nixpkgs" here + # which ended up on an older revision with an older cargo until there is + # a stable release of crate2nix that fixes V1 manifest parsing with the newer + # cargo, at which time this whole note may be removed. + nixpkgs_stable = builtins.fetchTree (flakeInput "nixpkgs"); in -{ pkgs ? import ./nixpkgs.nix { } - # Use last pinned crate2nix packages to build the test runner - # so that it works even if we have broken stuff! -, tools ? pkgs.callPackage "${builtins.fetchTree flakeLock.nodes.crate2nix_stable.locked}/tools.nix" { } +{ + # A system must be specified if using default value for pkgs and calling this + # package from a pure evaluation context, such as from the flake devShell. + system ? builtins.currentSystem +, pkgs ? import nixpkgs_stable { inherit system; } +, tools ? pkgs.callPackage "${crate2nix_stable}/tools.nix" { } }: let nixTestRunner = tools.appliedCargoNix {