From b3254b103869ff7cb84a4eaca7c45233e49d1bdf Mon Sep 17 00:00:00 2001 From: Qubasa Date: Sun, 21 Apr 2024 21:22:47 +0200 Subject: [PATCH] Add --system-option flag. --- disko-install | 29 +++++++++++++++++++++++++++++ install-cli.nix | 43 +++++++++++++++++++++++-------------------- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/disko-install b/disko-install index 24f53ea..9d95eb3 100755 --- a/disko-install +++ b/disko-install @@ -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") diff --git a/install-cli.nix b/install-cli.nix index 0310921..f0a7909 100644 --- a/install-cli.nix +++ b/install-cli.nix @@ -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,19 +17,21 @@ let else throw "No device passed for disk '${name}'. Pass `--disk ${name} /dev/name` via commandline"; - modifiedDisks = builtins.mapAttrs ( - name: value: - let - dev = deviceName name; - in - value - // { - device = dev; - content = value.content // { - device = dev; - }; - } - ) originalSystem.config.disko.devices.disk; + modifiedDisks = builtins.mapAttrs + ( + name: value: + let + dev = deviceName name; + in + value + // { + device = dev; + content = value.content // { + device = dev; + }; + } + ) + 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 ) ]; };