Skip to content

Commit d9035b9

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 bb8b38c commit d9035b9

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

src/nixos-anywhere.sh

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,23 +1050,55 @@ SSH
10501050
10511051
# Get system-features with a specific cpu architecture from the machine and add them to the installer
10521052
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
1053+
system_features=$(runSshNoTty -o ConnectTimeout=10 nix --extra-experimental-features 'nix-command' config show system-features 2>/dev/null || true)
1054+
1055+
# First, try to evaluate all nix settings from the flake in one go
1056+
nixConfContent=$(nix --extra-experimental-features 'nix-command flakes' eval --raw --apply "
1057+
config:
1058+
let
1059+
settings = config.nix.settings or {};
1060+
gccArch = config.nixpkgs.hostPlatform.gcc.arch or null;
1061+
1062+
# Check if system-features are defined in configuration
1063+
configFeatures = settings.system-features or null;
1064+
hasConfigFeatures = configFeatures != null && configFeatures != [];
1065+
1066+
remoteFeatures = let
1067+
remoteFeaturesStr = \"${system_features}\";
1068+
# Parse remote features string (space-separated) into list
1069+
remoteFeaturesList = if remoteFeaturesStr != \"\" then
1070+
builtins.filter (x: x != \"\") (builtins.split \" +\" remoteFeaturesStr)
1071+
else [];
1072+
in remoteFeaturesList;
1073+
1074+
# Combine base features (config or remote) with platform-specific features
1075+
baseFeatures = if hasConfigFeatures then configFeatures else remoteFeatures;
1076+
# At least one of nix.settings.system-features or nixpkgs.hostPlatform.gcc.arch has been explicitly defined
1077+
allFeatures = if (gccArch != null) || hasConfigFeatures then baseFeatures ++ (if gccArch != null then [\"gccarch-\${gccArch}\"] else []) else [];
1078+
1079+
# Deduplicate using listToAttrs trick
1080+
uniqueFeatures = builtins.attrNames (builtins.listToAttrs (map (f: { name = f; value = true; }) allFeatures));
1081+
1082+
substituters = builtins.toString (settings.substituters or []);
1083+
trustedPublicKeys = builtins.toString (settings.trusted-public-keys or []);
1084+
systemFeatures = builtins.toString uniqueFeatures;
1085+
1086+
# Helper function for optional config lines
1087+
optionalLine = cond: line: if cond then line + \"\n\" else \"\";
1088+
useSubstituters = \"${machineSubstituters}\" == \"y\";
1089+
in
1090+
optionalLine (useSubstituters && substituters != \"\") \"extra-substituters = \${substituters}\"
1091+
+ optionalLine (useSubstituters && trustedPublicKeys != \"\") \"extra-trusted-public-keys = \${trustedPublicKeys}\"
1092+
+ optionalLine (systemFeatures != \"\") \"system-features = \${systemFeatures}\"
1093+
" "${flake}#${flakeAttr}")
1094+
1095+
# Write to nix.conf if we have any content
1096+
if [[ -n ${nixConfContent} ]]; then
1097+
runSsh sh <<SSH
10671098
mkdir -p ~/.config/nix
1068-
echo "system-features = ${system_features}" >> ~/.config/nix/nix.conf
1099+
echo "${nixConfContent}" >> ~/.config/nix/nix.conf
10691100
SSH
1101+
fi
10701102
fi
10711103
10721104
if [[ ${phases[disko]} == 1 ]]; then

0 commit comments

Comments
 (0)