From a937ebf3fcf9953a8b9e47b379639c0aeb08f311 Mon Sep 17 00:00:00 2001 From: Niklas Gollenstede Date: Thu, 24 Oct 2024 22:18:47 +0200 Subject: [PATCH] install-system: remove `diskPaths` positional arg, but add default for `--disks=` flag --- README.md | 2 +- example/hosts/example.nix.md | 4 ++-- lib/nixos.nix | 2 +- lib/setup-scripts/install.sh | 18 ++++++++++-------- lib/setup-scripts/maintenance.sh | 9 +++------ modules/setup/temproot.nix.md | 4 ++-- 6 files changed, 19 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7f9f61e..3bef6d1 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ A set of composable [`setup-scripts`](./lib/setup-scripts/) can then [automatica The only thing that the scripts will interactively prompt for are external secrets (e.g., passwords), iff required by the new host. When using [`mkSystemsFlake`](./lib/nixos.nix#mkSystemsFlake), the installation can be started with: ```bash -nix run .'#'hostname -- install-system /path/to/disk +nix run .'#'hostname -- install-system --disks=/path/to/disk ``` Help output with information on available commands and flags is [available here](https://github.com/NiklasGollenstede/nixos-installer/wiki/−−help-Output) or via: ```bash diff --git a/example/hosts/example.nix.md b/example/hosts/example.nix.md index a7cf538..401fdf8 100644 --- a/example/hosts/example.nix.md +++ b/example/hosts/example.nix.md @@ -9,11 +9,11 @@ Just to provide an example of what a host configuration using this set of librar To install the system to a (set of) virtual machine disk images, with `$hostname` as any of the `instances` below, run in `..`: ```bash - nix run .'#'$hostname -- install-system /tmp/$hostname/ + nix run .'#'$hostname -- install-system --disks=/tmp/$hostname/ ``` Then to boot the system in a qemu VM with KVM: ```bash - nix run .'#'$hostname -- run-qemu /tmp/$hostname/ + nix run .'#'$hostname -- run-qemu --disks=/tmp/$hostname/ ``` See `nix run .#$hostname -- --help` for options and more commands. diff --git a/lib/nixos.nix b/lib/nixos.nix index b39e55c..25f2151 100644 --- a/lib/nixos.nix +++ b/lib/nixos.nix @@ -208,7 +208,7 @@ in rec { Examples: Install the system »$host« to the image file »/tmp/system-$host.img«: - $ nix run .#$host -- install-system /tmp/system-$host.img + $ nix run .#$host -- install-system --disks=/tmp/system-$host.img Test a fresh installation of »$host« in a qemu VM: $ nix run .#$host -- run-qemu --install=always diff --git a/lib/setup-scripts/install.sh b/lib/setup-scripts/install.sh index 06d2076..68dc0d9 100644 --- a/lib/setup-scripts/install.sh +++ b/lib/setup-scripts/install.sh @@ -3,7 +3,7 @@ # NixOS Installation ## -declare-command install-system '[--disks=]diskPaths' << 'EOD' +declare-command install-system '[--disks=diskPaths]' << 'EOD' This command installs a NixOS system to local disks or image files. It gets all the information it needs from the system's NixOS configuration -- except for the path(s) of the target disk(s) / image file(s); see the »--disks=« flag. @@ -22,12 +22,16 @@ What the installation does is defined solely by the target host's NixOS configur The "Installation" section of each host's documentation should contain host specific details, if any. Various »FLAG«s below affect how the installation is performed (in VM, verbosity, debugging, ...). EOD -declare-flag install-system disks "diskPaths" "The disk(s) (to be) used by this system installation. +declare-flag install-system,'*' disks "diskPaths" "The disk(s) (to be) used by this system installation. If »diskPaths« points to something in »/dev/«, then it is directly used as block device, otherwise »diskPaths« is (re-)created as raw image file and then used as loop device. -For hosts that install to multiple disks, pass a :-separated list of »=« pairs (the name may be omitted only for the "default" disk)." -function install-system {( # 1: diskPaths +For hosts that install to multiple disks, pass a colon-separated list of »=« pairs (the name may be omitted only for the "default" disk). +If a directory path ending in a forward slash is passed, it is expanded to ».img« files in that directory, one per (and named after) declared disk. +Defaults to »/tmp/nixos-img-@{config.installer.outputName:-@{config.system.name}}/«." +if [[ ! ${args[disks]:-} ]] ; then args[disks]=/tmp/nixos-img-@{config.installer.outputName:-@{config.system.name}}/ ; fi + +function install-system {( trap - EXIT # start with empty traps for sub-shell - prepare-installer "$@" || exit + prepare-installer || exit do-disk-setup || exit install-system-to $mnt || exit )} @@ -40,12 +44,10 @@ See also the »--no-vm« and »--vm-shared=« flags." declare-flag install-system no-vm "" "Never perform the installation in a VM. Fail if not executed as »root«." ## Does some argument validation, performs some sanity checks, includes a hack to make installation work when nix isn't installed for root, and runs the installation in qemu (if requested). -function prepare-installer { # 1: diskPaths +function prepare-installer { run-hook-script 'Prepare Installer' @{config.installer.commands.prepareInstaller!writeText.prepareInstallerCommands} || exit - if [[ ! ${args[disks]:-} ]] ; then args[disks]=${1:?"The --disks= flag or the first positional argument must specify the path(s) to the disk(s) and/or image file(s) to install to"} ; shift ; fi - umask g-w,o-w # Ensure that files created without explicit permissions are not writable for group and other. if [[ "$(id -u)" != '0' ]] ; then diff --git a/lib/setup-scripts/maintenance.sh b/lib/setup-scripts/maintenance.sh index eba215e..232f217 100644 --- a/lib/setup-scripts/maintenance.sh +++ b/lib/setup-scripts/maintenance.sh @@ -47,16 +47,15 @@ function register-vbox {( echo " ssh $(@{native.inetutils}/bin/hostname) VBoxManage controlvm $vmName screenshotpng /dev/stdout | display" )} -declare-command run-qemu '[--disks=]disks' qemuArgs... << 'EOD' +declare-command run-qemu '[--disks=diskPaths]' qemuArgs... << 'EOD' Runs a host in a QEMU VM, directly from its bootable disks, without requiring any change in it's configuration. This function infers many qemu options from the target system's configuration and the current host system. -»disks« may be passed in the same format as to the installer. Any image files passed are ensured to be loop-mounted. »root« may also pass device paths. EOD declare-flag run-qemu dry-run "" "Instead of running the (main) qemu (and install) command, only print it." declare-flag run-qemu efi "" "Treat the target system as EFI system, even if not recognized as such automatically." declare-flag run-qemu efi-vars "path" "For »--efi« systems, path to a file storing the EFI variables. The default is in »XDG_RUNTIME_DIR«, i.e. it does not persist across host reboots." declare-flag run-qemu graphic "" "Open a graphical window even of the target system logs to serial and not (explicitly) TTY1." -declare-flag run-qemu install "[1|always]" "If any of the guest system's disk images does not exist, perform the its installation before starting the VM. If set to »always«, always install before starting the VM. With this flag set, »disks« defaults to paths in »/tmp/." +declare-flag run-qemu install "[1|always]" "If any of the guest system's disk images does not exist, perform the its installation before starting the VM. If set to »always«, always install before starting the VM." declare-flag run-qemu no-kvm "" "Do not try to use (or complain about the unavailability of) KVM." declare-flag run-qemu nat-fw "forwards" "Port forwards to the guest's NATed NIC. E.g: »--nat-fw=:8000-:8000,:8001-:8001,127.0.0.1:2022-:22«." declare-flag run-qemu no-nat "" "Do not provide a NATed NIC to the guest." @@ -70,9 +69,7 @@ $ ... --nic=vde,sock=/tmp/vm-net # multiple times" declare-flag run-qemu no-serial "" "Do not connect the calling terminal to a serial adapter the guest can log to and open a terminal on the guests serial, as would be the default if the guests logs to ttyS0." declare-flag run-qemu share "decls" "Host dirs to make available as network shares for the guest, as space separated list of »name:host-path,options«. E.g. »--share='foo:/home/user/foo,readonly=on bar:/tmp/bar«. In the VM the share can be mounted with: »$ mount -t 9p -o trans=virtio -o version=9p2000.L -o msize=4194304 -o ro foo /foo«." declare-flag run-qemu virtio-blk "" "Pass the system's disks/images as virtio disks, instead of using AHCI+IDE. Default iff »boot.initrd.availableKernelModules« includes »virtio_blk« (because it requires that driver)." -function run-qemu { # 1?: disks, ...: qemuArgs - if [[ ${args[install]:-} && ! ${1:-} && ! ${args[disks]:-} ]] ; then args[disks]=/tmp/nixos-vm/@{config.installer.outputName:-@{config.system.name}}/ ; fi - if [[ ! ${args[disks]:-} ]] ; then args[disks]=${1:?"The --disks= flag or the first positional argument must specify the path(s) to the disk(s) and/or image file(s) to install to, or the --install flag must be set to enable an automatic fallback"} ; shift ; fi +function run-qemu { # ...: qemuArgs local qemu=( ) diff --git a/modules/setup/temproot.nix.md b/modules/setup/temproot.nix.md index 4f9d91d..2074dc1 100644 --- a/modules/setup/temproot.nix.md +++ b/modules/setup/temproot.nix.md @@ -245,7 +245,7 @@ in { keystore.keys."luks/swap-${hash}/0" = lib.mkIf cfg.swap.encrypted (lib.mkOptionDefault "random"); }; swapDevices = [ { inherit device; } ]; - boot.resumeDevice = device; + boot.resumeDevice = lib.mkDefault device; })) (lib.mkIf (cfg.swap.size != null && !cfg.swap.asPartition) { @@ -346,7 +346,7 @@ in { })) (lib.mkIf (cfg.remote.type == "none") { - systemd.tmpfiles.rules = [ (lib.fun.mkTmpfile { type = "L+"; path = "/remote"; argument = "/local"; }) ]; # for compatibility (but use a symlink to make clear that this is not actually a separate mount) + systemd.tmpfiles.rules = [ (lib.fun.mkTmpfile { type = "L+"; path = "/remote"; argument = "local"; }) ]; # for compatibility (but use a symlink to make clear that this is not actually a separate mount) }) ] ++ (map (type: (lib.mkIf (cfg.${type}.type == "bind") {