Skip to content

Commit

Permalink
Use a fixed Nix version
Browse files Browse the repository at this point in the history
This makes the binary always use the same Nix version, instead of getting
it from PATH. This makes it more reproducible, because issues from
different Nix versions can't occur anymore,
e.g. #78
  • Loading branch information
infinisil committed Jul 19, 2024
1 parent 30d4f4d commit f55f7a6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
7 changes: 7 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ let
settings.formatter.shfmt.options = [ "--space-redirects" ];
};

# The resulting package is built to always use this Nix version, such that the result is reproducible
# TODO: Switch this to pkgs.nixVersions.minimum, because that's what Nixpkgs CI should use
defaultNixPackage = pkgs.nix;

packages = {
build = pkgs.callPackage ./package.nix {
inherit
Expand All @@ -54,10 +58,12 @@ let
testNixpkgsPath
version
;
nix = defaultNixPackage;
};

shell = pkgs.mkShell {
env.NIX_CHECK_BY_NAME_EXPR_PATH = toString runtimeExprPath;
env.NIX_CHECK_BY_NAME_NIX_PACKAGE = lib.getBin defaultNixPackage;
env.NIX_PATH = "test-nixpkgs=${toString testNixpkgsPath}:test-nixpkgs/lib=${toString nixpkgsLibPath}";
env.RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}";
inputsFrom = [ packages.build ];
Expand All @@ -69,6 +75,7 @@ let
rust-analyzer
rustfmt
treefmtEval.config.build.wrapper
defaultNixPackage
];
};

Expand Down
4 changes: 3 additions & 1 deletion package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ rustPlatform.buildRustPackage {
makeWrapper
];
env.NIX_CHECK_BY_NAME_EXPR_PATH = "${runtimeExprPath}";
env.NIX_CHECK_BY_NAME_NIX_PACKAGE = lib.getBin nix;
env.NIX_PATH = "test-nixpkgs=${testNixpkgsPath}:test-nixpkgs/lib=${nixpkgsLibPath}";
preCheck = initNix;
postCheck = ''
Expand All @@ -41,6 +42,7 @@ rustPlatform.buildRustPackage {
'';
postInstall = ''
wrapProgram $out/bin/nixpkgs-check-by-name \
--set NIX_CHECK_BY_NAME_EXPR_PATH "$NIX_CHECK_BY_NAME_EXPR_PATH"
--set NIX_CHECK_BY_NAME_EXPR_PATH "$NIX_CHECK_BY_NAME_EXPR_PATH" \
--set NIX_CHECK_BY_NAME_NIX_PACKAGE ${lib.getBin nix}
'';
}
6 changes: 5 additions & 1 deletion src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ pub fn check_values(
let expr_path = std::env::var("NIX_CHECK_BY_NAME_EXPR_PATH")
.with_context(|| "Could not get environment variable NIX_CHECK_BY_NAME_EXPR_PATH")?;

// Pinning nix in this way makes the tool more reproducible
let nix_package = std::env::var("NIX_CHECK_BY_NAME_NIX_PACKAGE")
.with_context(|| "Could not get environment variable NIX_CHECK_BY_NAME_NIX_PACKAGE")?;

// With restrict-eval, only paths in NIX_PATH can be accessed. We explicitly specify them here.
let mut command = process::Command::new("nix-instantiate");
let mut command = process::Command::new(format!("{nix_package}/bin/nix-instantiate"));
command
// Capture stderr so that it can be printed later in case of failure
.stderr(process::Stdio::piped())
Expand Down

0 comments on commit f55f7a6

Please sign in to comment.