Skip to content

Commit b5793e8

Browse files
sabifyMic92
andcommitted
feat: simplify and unify writing in nix.conf
Co-authored-by: Jörg Thalheim <Mic92@users.noreply.github.com>
1 parent d229c4e commit b5793e8

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

src/nixos-anywhere.sh

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,37 +1036,56 @@ main() {
10361036
sshConnection="root@${sshHost}"
10371037
fi
10381038
1039-
# Get substituters from the machine and add them to the installer
1040-
if [[ ${machineSubstituters} == "y" && -n ${flake} ]]; then
1041-
substituters=$(nix --extra-experimental-features 'nix-command flakes' eval --apply toString "${flake}"#"${flakeAttr}".nix.settings.substituters)
1042-
trustedPublicKeys=$(nix --extra-experimental-features 'nix-command flakes' eval --apply toString "${flake}"#"${flakeAttr}".nix.settings.trusted-public-keys)
1043-
1044-
runSsh sh <<SSH || true
1045-
mkdir -p ~/.config/nix
1046-
echo "extra-substituters = ${substituters}" >> ~/.config/nix/nix.conf
1047-
echo "extra-trusted-public-keys = ${trustedPublicKeys}" >> ~/.config/nix/nix.conf
1048-
SSH
1049-
fi
1050-
1051-
# Get system-features with a specific cpu architecture from the machine and add them to the installer
10521039
if [[ -n ${flake} ]]; then
1053-
system_features=$(nix --extra-experimental-features 'nix-command flakes' eval --apply 'x: toString (x.nix.settings.system-features or "")' "${flake}#${flakeAttr}")
1054-
if [[ -z ${system_features} ]]; then
1055-
system_features=$(runSshNoTty -o ConnectTimeout=10 nix --extra-experimental-features 'nix-command' config show system-features)
1056-
fi
1057-
1058-
platform_arch=$(nix --extra-experimental-features 'nix-command flakes' eval --apply 'x: toString (x.nixpkgs.hostPlatform.gcc.arch or "")' "${flake}#${flakeAttr}")
1059-
if [[ -n ${platform_arch} ]]; then
1060-
system_features="${system_features} gccarch-${platform_arch}"
1061-
fi
1062-
1063-
# deduplicate the features
1064-
system_features=$(echo "${system_features}" | tr ' ' '\n' | sort -u | tr '\n' ' ' | sed 's/ $//')
1065-
1066-
runSsh sh <<SSH
1040+
system_features=$(runSshNoTty -o ConnectTimeout=10 nix --extra-experimental-features 'nix-command' config show system-features 2>/dev/null || true)
1041+
1042+
# First, try to evaluate all nix settings from the flake in one go
1043+
nixConfContent=$(nix --extra-experimental-features 'nix-command flakes' eval --raw --apply "
1044+
config:
1045+
let
1046+
settings = config.nix.settings or {};
1047+
gccArch = config.nixpkgs.hostPlatform.gcc.arch or null;
1048+
1049+
# Check if system-features are defined in configuration
1050+
configFeatures = settings.system-features or null;
1051+
hasConfigFeatures = configFeatures != null && configFeatures != [];
1052+
1053+
remoteFeatures = let
1054+
remoteFeaturesStr = \"${system_features}\";
1055+
# Parse remote features string (space-separated) into list
1056+
remoteFeaturesList = if remoteFeaturesStr != \"\" then
1057+
builtins.filter (x: x != \"\") (builtins.split \" +\" remoteFeaturesStr)
1058+
else [];
1059+
in remoteFeaturesList;
1060+
1061+
# Combine base features (config or remote) with platform-specific features
1062+
baseFeatures = if hasConfigFeatures then configFeatures else remoteFeatures;
1063+
# At least one of nix.settings.system-features or nixpkgs.hostPlatform.gcc.arch has been explicitly defined
1064+
allFeatures = if (gccArch != null) || hasConfigFeatures then baseFeatures ++ (if gccArch != null then [\"gccarch-\${gccArch}\"] else []) else [];
1065+
1066+
# Deduplicate using listToAttrs trick
1067+
uniqueFeatures = builtins.attrNames (builtins.listToAttrs (map (f: { name = f; value = true; }) allFeatures));
1068+
1069+
substituters = builtins.toString (settings.substituters or []);
1070+
trustedPublicKeys = builtins.toString (settings.trusted-public-keys or []);
1071+
systemFeatures = builtins.toString uniqueFeatures;
1072+
1073+
# Helper function for optional config lines
1074+
optionalLine = cond: line: if cond then line + \"\n\" else \"\";
1075+
useSubstituters = \"${machineSubstituters}\" == \"y\";
1076+
in
1077+
optionalLine (useSubstituters && substituters != \"\") \"extra-substituters = \${substituters}\"
1078+
+ optionalLine (useSubstituters && trustedPublicKeys != \"\") \"extra-trusted-public-keys = \${trustedPublicKeys}\"
1079+
+ optionalLine (systemFeatures != \"\") \"system-features = \${systemFeatures}\"
1080+
" "${flake}#${flakeAttr}")
1081+
1082+
# Write to nix.conf if we have any content
1083+
if [[ -n ${nixConfContent} ]]; then
1084+
runSsh sh <<SSH
10671085
mkdir -p ~/.config/nix
1068-
echo "system-features = ${system_features}" >> ~/.config/nix/nix.conf
1086+
echo "${nixConfContent}" >> ~/.config/nix/nix.conf
10691087
SSH
1088+
fi
10701089
fi
10711090
10721091
if [[ ${phases[disko]} == 1 ]]; then

0 commit comments

Comments
 (0)