Add --system-option flag.

This commit is contained in:
Qubasa 2024-04-21 21:22:47 +02:00 committed by mergify[bot]
parent 285e26465a
commit b3254b1038
2 changed files with 52 additions and 20 deletions

View file

@ -19,6 +19,10 @@ Usage: $0 [OPTIONS]
--write-efi-boot-entries Write EFI boot entries to the NVRAM of the system for the installed system.
Specify this option if you plan to boot from this disk on the current machine,
but not if you plan to move the disk to another machine.
--system-option KEY VALUE Pass the specified system option to the NixOS configuration.
This option can be specified multiple times.
For example, to set the authorizedKeys, use
--system-option users.users.root.openssh.authorizedKeys.keys '[ "ssh-rsa ..." ]'
EOF
}
@ -35,6 +39,19 @@ serialiaseArrayToNix() {
echo "$nixExpr"
}
serialiaseArrayToNixUnquoted() {
local -n array=$1
nixExpr="{ "
# Iterate over the associative array to populate the Nix attrset string
for key in "${!array[@]}"; do
value=${array[$key]}
nixExpr+="${key} = ${value};"
done
nixExpr+="}"
echo "$nixExpr"
}
readonly libexec_dir="${0%/*}"
nix_args=(
@ -46,6 +63,7 @@ diskoAttr=diskoScript
writeEfiBootEntries=false
declare -A diskMappings
declare -A extraFiles
declare -A extraSystemConfig
parseArgs() {
[[ $# -eq 0 ]] && {
@ -95,6 +113,16 @@ parseArgs() {
esac
shift
;;
--system-option)
if [[ $# -lt 3 ]]; then
echo "Option $1 requires two arguments: key, value" >&2
exit 1
fi
# shellcheck disable=SC2034
extraSystemConfig[$2]=$3
shift
shift
;;
--extra-files)
if [[ $# -lt 3 ]]; then
echo "Option $1 requires two arguments: source, destination" >&2
@ -193,6 +221,7 @@ main() {
--argstr rootMountPoint "$mountPoint" \
--arg writeEfiBootEntries "$writeEfiBootEntries" \
--arg diskMappings "$(serialiaseArrayToNix diskMappings)" \
--arg extraSystemConfig "$(serialiaseArrayToNixUnquoted extraSystemConfig)" \
-A installToplevel \
-A "$diskoAttr")

View file

@ -1,9 +1,10 @@
{
flake,
flakeAttr,
diskMappings,
writeEfiBootEntries ? false,
rootMountPoint ? "/mnt",
{ flake
, flakeAttr
, diskMappings
, extraSystemConfig ? { }
, writeEfiBootEntries ? false
, rootMountPoint ? "/mnt"
,
}:
let
originalSystem = (builtins.getFlake "${flake}").nixosConfigurations."${flakeAttr}";
@ -16,7 +17,8 @@ let
else
throw "No device passed for disk '${name}'. Pass `--disk ${name} /dev/name` via commandline";
modifiedDisks = builtins.mapAttrs (
modifiedDisks = builtins.mapAttrs
(
name: value:
let
dev = deviceName name;
@ -28,7 +30,8 @@ let
device = dev;
};
}
) originalSystem.config.disko.devices.disk;
)
originalSystem.config.disko.devices.disk;
# filter all nixos module internal attributes
cleanedDisks = lib.filterAttrsRecursive (n: _: !lib.hasPrefix "_" n) modifiedDisks;
@ -49,7 +52,7 @@ let
{
boot.loader.efi.canTouchEfiVariables = lib.mkVMOverride writeEfiBootEntries;
boot.loader.grub.devices = lib.mkVMOverride diskoSystem.config.boot.loader.grub.devices;
}
} // extraSystemConfig
)
];
};