Skip to content

Commit

Permalink
replaceVarsWith: init
Browse files Browse the repository at this point in the history
Takes the extended features of nix substituteAll to a replaceVars
variant to get rid of those cases that use substituteAll to build a full
package with meta information etc.
  • Loading branch information
wolfgangwalther committed Dec 1, 2024
1 parent 4e0999e commit 3e8137e
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 161 deletions.
2 changes: 1 addition & 1 deletion nixos/modules/installer/tools/tools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let
manPage = ./manpages/nixos-version.8;
};

nixos-install = pkgs.nixos-install.override { nix = config.nix.package; };
nixos-install = pkgs.nixos-install.override { };
nixos-rebuild = pkgs.nixos-rebuild.override { nix = config.nix.package; };

defaultConfigTemplate = ''
Expand Down
75 changes: 46 additions & 29 deletions pkgs/build-support/replace-vars/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
replaceVars ./greeting.txt { world = "hello"; }
```
`replaceVarsWith <path> <drvAttrs> <attrs>` has an additional argument `drvAttrs` to override derivation arguments.
See `../../test/replace-vars/default.nix` for tests of this function.
*/
path: attrs:
path: drvAttrs: attrs:

let
# We use `--replace-fail` instead of `--subst-var-by` so that if the thing isn't there, we fail.
Expand All @@ -45,33 +47,48 @@ let
replacements = lib.concatLists (lib.mapAttrsToList subst-var-by attrs);
in

stdenvNoCC.mkDerivation {
name = baseNameOf (toString path);
src = path;
doCheck = true;
dontUnpack = true;
preferLocalBuild = true;
allowSubstitutes = false;

buildPhase = ''
runHook preBuild
substitute "$src" "$out" ${lib.concatStringsSep " " replacements}
runHook postBuild
'';

# Look for Nix identifiers surrounded by `@` that aren't substituted.
checkPhase =
let
regex = lib.escapeShellArg "@[a-zA-Z_][0-9A-Za-z_'-]*@";
in
''
runHook preCheck
if grep -qe ${regex} "$out"; then
echo The following look like unsubstituted Nix identifiers that remain in "$out":
grep -oe ${regex} "$out"
echo Use the more precise '`substitute`' function if this check is in error.
exit 1
stdenvNoCC.mkDerivation (
{
name = baseNameOf (toString path);
src = path;
doCheck = true;
dontUnpack = true;
preferLocalBuild = true;
allowSubstitutes = false;

buildPhase = ''
runHook preBuild
target=$out
if test -n "$dir"; then
target=$out/$dir/$name
mkdir -p $out/$dir
fi
substitute "$src" "$target" ${lib.concatStringsSep " " replacements}
if test -n "$isExecutable"; then
chmod +x $target
fi
runHook postCheck
runHook postBuild
'';
}

# Look for Nix identifiers surrounded by `@` that aren't substituted.
checkPhase =
let
regex = lib.escapeShellArg "@[a-zA-Z_][0-9A-Za-z_'-]*@";
in
''
runHook preCheck
if grep -qe ${regex} "$out"; then
echo The following look like unsubstituted Nix identifiers that remain in "$out":
grep -oe ${regex} "$out"
echo Use the more precise '`substitute`' function if this check is in error.
exit 1
fi
runHook postCheck
'';
}
// drvAttrs
)
1 change: 1 addition & 0 deletions pkgs/build-support/replace-vars/short.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ replaceVarsWith }: path: attrs: replaceVarsWith path { } attrs
2 changes: 1 addition & 1 deletion pkgs/by-name/de/deterministic-uname/deterministic-uname.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! @shell@
#! @runtimeShell@

set -o errexit
set -o nounset
Expand Down
43 changes: 21 additions & 22 deletions pkgs/by-name/de/deterministic-uname/package.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
# expr and script based on our lsb_release
{ stdenv
, lib
, substituteAll
, replaceVarsWith
, coreutils
, getopt
, runtimeShell
, modDirVersion ? ""
, forPlatform ? stdenv.buildPlatform
}:

substituteAll {
replaceVarsWith ./deterministic-uname.sh {
name = "uname";

src = ./deterministic-uname.sh;

dir = "bin";
isExecutable = true;

inherit coreutils getopt;
meta = with lib; {
description = "Print certain system information (hardcoded with lib/system values)";
mainProgram = "uname";
longDescription = ''
This package provides a replacement for `uname` whose output depends only
on `stdenv.buildPlatform`, or a configurable `forPlatform`. It is meant
to be used from within derivations. Many packages' build processes run
`uname` at compile time and embed its output into the result of the build.
Since `uname` calls into the kernel, and the Nix sandbox currently does
not intercept these calls, builds made on different kernels will produce
different results.
'';
license = [ licenses.mit ];
maintainers = with maintainers; [ artturin ];
platforms = platforms.all;
};
} {
inherit coreutils getopt runtimeShell;

uSystem = if forPlatform.uname.system != null then forPlatform.uname.system else "unknown";
inherit (forPlatform.uname) processor;
Expand All @@ -39,21 +55,4 @@ substituteAll {
# --replace '$(shell uname -r)' "${kernel.modDirVersion}" \
# is a common thing to do.
modDirVersion = if modDirVersion != "" then modDirVersion else "unknown";

meta = with lib; {
description = "Print certain system information (hardcoded with lib/system values)";
mainProgram = "uname";
longDescription = ''
This package provides a replacement for `uname` whose output depends only
on `stdenv.buildPlatform`, or a configurable `forPlatform`. It is meant
to be used from within derivations. Many packages' build processes run
`uname` at compile time and embed its output into the result of the build.
Since `uname` calls into the kernel, and the Nix sandbox currently does
not intercept these calls, builds made on different kernels will produce
different results.
'';
license = [ licenses.mit ];
maintainers = with maintainers; [ artturin ];
platforms = platforms.all;
};
}
2 changes: 1 addition & 1 deletion pkgs/by-name/ls/lsb-release/lsb_release.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! @shell@
#! @runtimeShell@

set -o errexit
set -o nounset
Expand Down
12 changes: 5 additions & 7 deletions pkgs/by-name/ls/lsb-release/package.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
{ substituteAll, lib
, coreutils, getopt
{ replaceVarsWith, lib
, coreutils, getopt, runtimeShell
}:

substituteAll {
replaceVarsWith ./lsb_release.sh {
name = "lsb_release";

src = ./lsb_release.sh;

dir = "bin";
isExecutable = true;

inherit coreutils getopt;

meta = with lib; {
description = "Prints certain LSB (Linux Standard Base) and Distribution information";
mainProgram = "lsb_release";
license = [ licenses.mit ];
maintainers = with maintainers; [ primeos ];
platforms = platforms.linux;
};
} {
inherit coreutils getopt runtimeShell;
}
30 changes: 16 additions & 14 deletions pkgs/by-name/ni/nixos-build-vms/package.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
{
substituteAll,
replaceVarsWith,
runtimeShell,
installShellFiles,
}:
substituteAll {
name = "nixos-build-vms";
src = ./nixos-build-vms.sh;
inherit runtimeShell;
buildVms = ./build-vms.nix;
replaceVarsWith ./nixos-build-vms.sh
{
name = "nixos-build-vms";

dir = "bin";
isExecutable = true;
dir = "bin";
isExecutable = true;

nativeBuildInputs = [ installShellFiles ];
nativeBuildInputs = [ installShellFiles ];

postInstall = ''
installManPage ${./nixos-build-vms.8}
'';
postInstall = ''
installManPage ${./nixos-build-vms.8}
'';

meta.mainProgram = "nixos-build-vms";
}
meta.mainProgram = "nixos-build-vms";
}
{
inherit runtimeShell;
buildVms = ./build-vms.nix;
}
2 changes: 1 addition & 1 deletion pkgs/by-name/ni/nixos-container/nixos-container.pl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Cwd 'abs_path';
use Time::HiRes;

my $nsenter = "@utillinux@/bin/nsenter";
my $nsenter = "@util-linux@/bin/nsenter";
my $su = "@su@";

my $configurationDirectory = "@configurationDirectory@";
Expand Down
53 changes: 26 additions & 27 deletions pkgs/by-name/ni/nixos-container/package.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ substituteAll
{ replaceVarsWith
, perl
, shadow
, util-linux
Expand All @@ -7,33 +7,32 @@
, nixosTests
}:

substituteAll {
name = "nixos-container";
dir = "bin";
isExecutable = true;
src = ./nixos-container.pl;
perl = perl.withPackages (p: [ p.FileSlurp ]);
su = "${shadow.su}/bin/su";
utillinux = util-linux;
replaceVarsWith ./nixos-container.pl {
name = "nixos-container";
dir = "bin";
isExecutable = true;

inherit configurationDirectory stateDirectory;

passthru = {
tests = {
inherit (nixosTests)
containers-imperative
containers-ip
containers-tmpfs
containers-ephemeral
containers-unified-hierarchy
;
};
passthru = {
tests = {
inherit (nixosTests)
containers-imperative
containers-ip
containers-tmpfs
containers-ephemeral
containers-unified-hierarchy
;
};
};

postInstall = ''
t=$out/share/bash-completion/completions
mkdir -p $t
cp ${./nixos-container-completion.sh} $t/nixos-container
'';
meta.mainProgram = "nixos-container";
} {
perl = perl.withPackages (p: [ p.FileSlurp ]);
su = "${shadow.su}/bin/su";

postInstall = ''
t=$out/share/bash-completion/completions
mkdir -p $t
cp ${./nixos-container-completion.sh} $t/nixos-container
'';
meta.mainProgram = "nixos-container";
inherit configurationDirectory stateDirectory util-linux;
}
47 changes: 24 additions & 23 deletions pkgs/by-name/ni/nixos-enter/package.nix
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
{
lib,
substituteAll,
replaceVarsWith,
runtimeShell,
installShellFiles,
util-linuxMinimal,
}:
substituteAll {
name = "nixos-enter";
src = ./nixos-enter.sh;
replaceVarsWith ./nixos-enter.sh
{
name = "nixos-enter";

inherit runtimeShell;
dir = "bin";
isExecutable = true;

path = lib.makeBinPath [
util-linuxMinimal
];
nativeBuildInputs = [ installShellFiles ];

dir = "bin";
isExecutable = true;
postInstall = ''
installManPage ${./nixos-enter.8}
'';

nativeBuildInputs = [ installShellFiles ];
meta = {
description = "Run a command in a NixOS chroot environment";
homepage = "https://github.com/NixOS/nixpkgs/tree/master/pkgs/by-name/ni/nixos-install";
license = lib.licenses.mit;
platforms = lib.platforms.linux;
mainProgram = "nixos-enter";
};
}
{
inherit runtimeShell;

postInstall = ''
installManPage ${./nixos-enter.8}
'';

meta = {
description = "Run a command in a NixOS chroot environment";
homepage = "https://github.com/NixOS/nixpkgs/tree/master/pkgs/by-name/ni/nixos-install";
license = lib.licenses.mit;
platforms = lib.platforms.linux;
mainProgram = "nixos-enter";
};
}
path = lib.makeBinPath [
util-linuxMinimal
];
}
Loading

0 comments on commit 3e8137e

Please sign in to comment.