WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 5dea800

Browse files
committed
Add options to override default substituters and trusted-public-keys
This commit introduces a way to override the default substituters and trusted-public-keys copied from the reference machine. A common use case is when the reference machine points to a local "proxy" binary cache (e.g., 127.0.0.1), which will not work on the target host. Using `--no-use-machine-substituters` disables the feature altogether, while `--option substituters <list>` and `--option extra-substituters <list>` results in errors due to the user not being part of trusted-users. This commit also changes how `~/.config/nix/nix.conf` is created: it now stores a backup (with a `.orig` suffix) before patching the file, and reuses that backup on future invocations. This avoids the situation where multiple runs of `nixos-anywhere` repeatedly append the same configuration over and over.
1 parent bad98b0 commit 5dea800

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/nixos-anywhere.sh

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ nixOptions=(
2222
)
2323
SSH_PRIVATE_KEY=${SSH_PRIVATE_KEY-}
2424
machineSubstituters="y"
25+
substituters=""
26+
trustedPublicKeys=""
2527

2628
declare -A phases
2729
phases[kexec]=1
@@ -167,6 +169,10 @@ Options:
167169
implies --no-use-machine-substituters
168170
* --no-use-machine-substituters
169171
don't copy the substituters from the machine to be installed into the installer environment
172+
* --substituters
173+
override default substituters copied to the installer environment
174+
* --trusted-public-keys
175+
override default trusted public keys copied to the installer environment
170176
* --debug
171177
enable debug output
172178
* --show-trace
@@ -388,6 +394,14 @@ parseArgs() {
388394
--no-use-machine-substituters)
389395
machineSubstituters=n
390396
;;
397+
--substituters)
398+
substituters="$2"
399+
shift
400+
;;
401+
--trusted-public-keys)
402+
trustedPublicKeys="$2"
403+
shift
404+
;;
391405
--build-on-remote)
392406
echo "WARNING: --build-on-remote is deprecated, use --build-on remote instead" 2>&1
393407
buildOnRemote=y
@@ -1038,13 +1052,28 @@ main() {
10381052
10391053
# Get substituters from the machine and add them to the installer
10401054
if [[ ${machineSubstituters} == "y" && -n ${flake} ]]; then
1041-
substituters=$(nix eval "${nixOptions[@]}" --apply toString "${flake}"#"${flakeAttr}".nix.settings.substituters)
1042-
trustedPublicKeys=$(nix eval "${nixOptions[@]}" --apply toString "${flake}"#"${flakeAttr}".nix.settings.trusted-public-keys)
1055+
if [[ -z ${substituters} ]]; then
1056+
substituters=$(nix eval "${nixOptions[@]}" --apply toString "${flake}"#"${flakeAttr}".nix.settings.substituters)
1057+
fi
1058+
if [[ -z ${trustedPublicKeys} ]]; then
1059+
trustedPublicKeys=$(nix eval "${nixOptions[@]}" --apply toString "${flake}"#"${flakeAttr}".nix.settings.trusted-public-keys)
1060+
fi
1061+
fi
10431062
1063+
if [[ -n ${substituters} ]] || [[ -n ${trustedPublicKeys} ]]; then
10441064
runSsh sh <<SSH || true
10451065
mkdir -p ~/.config/nix
1046-
echo "extra-substituters = ${substituters}" >> ~/.config/nix/nix.conf
1047-
echo "extra-trusted-public-keys = ${trustedPublicKeys}" >> ~/.config/nix/nix.conf
1066+
if [ -f ~/.config/nix/nix.conf.orig ]; then
1067+
cp -v ~/.config/nix/nix.conf.orig ~/.config/nix/nix.conf
1068+
else
1069+
if [ -f ~/.config/nix/nix.conf ]; then
1070+
cp -v ~/.config/nix/nix.conf ~/.config/nix/nix.conf.orig
1071+
else
1072+
touch ~/.config/nix/nix.conf.orig
1073+
fi
1074+
fi
1075+
[ "${#substituters}" != 0 ] && echo "extra-substituters = ${substituters}" >> ~/.config/nix/nix.conf
1076+
[ "${#trustedPublicKeys}" != 0 ] && echo "extra-trusted-public-keys = ${trustedPublicKeys}" >> ~/.config/nix/nix.conf
10481077
SSH
10491078
fi
10501079

0 commit comments

Comments
 (0)