diff --git a/scripts/nix-push.in b/scripts/nix-push.in index 1c910438f..8ac31d501 100644 --- a/scripts/nix-push.in +++ b/scripts/nix-push.in @@ -107,7 +107,7 @@ foreach my $storePath (@storePaths) { # Construct a Nix expression that creates a Nix archive. my $nixexpr = "((import $dataDir/nix/corepkgs/nar/nar.nix) " . - "{storePath = builtins.toPath \"$storePath\"; system = \"@system@\"; hashAlgo = \"$hashAlgo\";}) "; + "{storePath = builtins.storePath \"$storePath\"; system = \"@system@\"; hashAlgo = \"$hashAlgo\";}) "; print NIX $nixexpr; } diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 50a641670..4b7702eff 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -482,6 +482,27 @@ static Expr prim_toPath(EvalState & state, const ATermVector & args) } +/* Allow a valid store path to be used in an expression. This is + useful in some generated expressions such as in nix-push, which + generates a call to a function with an already existing store path + as argument. You don't want to use `toPath' here because it copies + the path to the Nix store, which yields a copy like + /nix/store/newhash-oldhash-oldname. In the past, `toPath' had + special case behaviour for store paths, but that created weird + corner cases. */ +static Expr prim_storePath(EvalState & state, const ATermVector & args) +{ + PathSet context; + Path path = canonPath(coerceToPath(state, args[0], context)); + if (!isInStore(path)) + throw EvalError(format("path `%1%' is not in the Nix store") % path); + if (!store->isValidPath(path)) + throw EvalError(format("store path `%1%' is not valid") % path); + context.insert(toStorePath(path)); + return makeStr(path, context); +} + + static Expr prim_pathExists(EvalState & state, const ATermVector & args) { PathSet context; @@ -950,6 +971,7 @@ void EvalState::addPrimOps() // Paths addPrimOp("__toPath", 1, prim_toPath); + addPrimOp("__storePath", 1, prim_storePath); addPrimOp("__pathExists", 1, prim_pathExists); addPrimOp("baseNameOf", 1, prim_baseNameOf); addPrimOp("dirOf", 1, prim_dirOf);