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. --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, 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. 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 EOF
} }
@ -35,6 +39,19 @@ serialiaseArrayToNix() {
echo "$nixExpr" 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%/*}" readonly libexec_dir="${0%/*}"
nix_args=( nix_args=(
@ -46,6 +63,7 @@ diskoAttr=diskoScript
writeEfiBootEntries=false writeEfiBootEntries=false
declare -A diskMappings declare -A diskMappings
declare -A extraFiles declare -A extraFiles
declare -A extraSystemConfig
parseArgs() { parseArgs() {
[[ $# -eq 0 ]] && { [[ $# -eq 0 ]] && {
@ -95,6 +113,16 @@ parseArgs() {
esac esac
shift 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) --extra-files)
if [[ $# -lt 3 ]]; then if [[ $# -lt 3 ]]; then
echo "Option $1 requires two arguments: source, destination" >&2 echo "Option $1 requires two arguments: source, destination" >&2
@ -193,6 +221,7 @@ main() {
--argstr rootMountPoint "$mountPoint" \ --argstr rootMountPoint "$mountPoint" \
--arg writeEfiBootEntries "$writeEfiBootEntries" \ --arg writeEfiBootEntries "$writeEfiBootEntries" \
--arg diskMappings "$(serialiaseArrayToNix diskMappings)" \ --arg diskMappings "$(serialiaseArrayToNix diskMappings)" \
--arg extraSystemConfig "$(serialiaseArrayToNixUnquoted extraSystemConfig)" \
-A installToplevel \ -A installToplevel \
-A "$diskoAttr") -A "$diskoAttr")

View file

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