From af2da574be446497115ffa29125a0b96757e2f9b Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 24 Jan 2023 15:57:17 +0100 Subject: [PATCH 1/4] add support for another mountpoint than /mnt --- cli.nix | 2 ++ default.nix | 6 ++++-- disko | 8 +++++++- doc.nix | 5 ++++- module.nix | 10 +++++++++- types.nix | 28 ++++++++++++++-------------- 6 files changed, 40 insertions(+), 19 deletions(-) diff --git a/cli.nix b/cli.nix index 006779e..da3e536 100644 --- a/cli.nix +++ b/cli.nix @@ -3,10 +3,12 @@ , flake ? null , flakeAttr ? null , diskoFile ? null +, rootMountPoint ? "/mnt" , noDeps ? false , ... }@args: let disko = import ./. { + inherit rootMountPoint; lib = pkgs.lib; }; diff --git a/default.nix b/default.nix index ada33e4..3265f02 100644 --- a/default.nix +++ b/default.nix @@ -1,6 +1,8 @@ -{ lib ? import }: +{ lib ? import +, rootMountPoint ? "/mnt" +}: let - types = import ./types.nix { inherit lib; }; + types = import ./types.nix { inherit lib rootMountPoint; }; eval = cfg: lib.evalModules { modules = lib.singleton { # _file = toString input; diff --git a/disko b/disko index 10e2fc2..c84cf52 100755 --- a/disko +++ b/disko @@ -25,6 +25,8 @@ Options: pass value to nix-build. can be used to set disk-names for example * --argstr name value pass value to nix-build as string +* --root-mountpoint /mnt + where to mount the device tree * --dry-run just show the path to the script instead of running it * --debug @@ -54,7 +56,7 @@ while [[ $# -gt 0 ]]; do shift ;; -f | --flake) - flake="$2" + flake=$2 shift ;; --argstr | --arg) @@ -69,6 +71,10 @@ while [[ $# -gt 0 ]]; do --dry-run) dry_run=y ;; + --root-mountpoint) + nix_args+=(--arg rootMountPoint "$2") + shift + ;; --no-deps) nix_args+=(--arg noDeps true) ;; diff --git a/doc.nix b/doc.nix index ab2bedf..be556ab 100644 --- a/doc.nix +++ b/doc.nix @@ -1,7 +1,10 @@ { lib, nixosOptionsDoc, runCommand, fetchurl, pandoc }: let - types = import ./types.nix { inherit lib; }; + types = import ./types.nix { + inherit lib; + rootMountPoint = "/mnt"; + }; eval = lib.evalModules { modules = [ { diff --git a/module.nix b/module.nix index 806d397..66a828e 100644 --- a/module.nix +++ b/module.nix @@ -1,6 +1,9 @@ { config, lib, pkgs, ... }: let - types = import ./types.nix { inherit lib; }; + types = import ./types.nix { + inherit lib; + rootMountPoint = config.disko.rootMountPoint; + }; cfg = config.disko; in { options.disko = { @@ -9,6 +12,11 @@ in { default = {}; description = "The devices to set up"; }; + rootMountPoint = lib.mkOption { + type = lib.types.str; + default = "/mnt"; + description = "Where the device tree should be mounted by the mountScript"; + }; enableConfig = lib.mkOption { description = '' configure nixos with the specified devices diff --git a/types.nix b/types.nix index 5d7d337..e12c894 100644 --- a/types.nix +++ b/types.nix @@ -1,4 +1,4 @@ -{ lib }: +{ lib, rootMountPoint }: with lib; with builtins; @@ -223,7 +223,7 @@ rec { */ zapCreateMount = devices: '' set -efux - umount -Rv /mnt || : + umount -Rv "${rootMountPoint}" mnt || : for dev in ${toString (lib.catAttrs "device" (lib.attrValues devices.disk))}; do ${./disk-deactivate}/disk-deactivate "$dev" | bash -x @@ -354,8 +354,8 @@ rec { inherit config options; default = {}: { fs.${config.mountpoint} = '' - if ! findmnt ${config.fsType} "/mnt${config.mountpoint}" > /dev/null 2>&1; then - mount -t ${config.fsType} ${config.device} "/mnt${config.mountpoint}" \ + if ! findmnt ${config.fsType} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then + mount -t ${config.fsType} ${config.device} "${rootMountPoint}${config.mountpoint}" \ ${concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \ -o X-mount.mkdir fi @@ -434,8 +434,8 @@ rec { in { fs = subvolMounts.fs // optionalAttrs (!isNull config.mountpoint) { ${config.mountpoint} = '' - if ! findmnt ${dev} "/mnt${config.mountpoint}" > /dev/null 2>&1; then - mount ${dev} "/mnt${config.mountpoint}" \ + if ! findmnt ${dev} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then + mount ${dev} "${rootMountPoint}${config.mountpoint}" \ ${concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \ -o X-mount.mkdir fi @@ -524,8 +524,8 @@ rec { else null; in optionalAttrs (!isNull mountpoint) { fs.${mountpoint} = '' - if ! findmnt ${dev} "/mnt${mountpoint}" > /dev/null 2>&1; then - mount ${dev} "/mnt${mountpoint}" \ + if ! findmnt ${dev} "${rootMountPoint}${mountpoint}" > /dev/null 2>&1; then + mount ${dev} "${rootMountPoint}${mountpoint}" \ ${concatMapStringsSep " " (opt: "-o ${opt}") (config.mountOptions ++ [ "subvol=${config.name}" ])} \ -o X-mount.mkdir fi @@ -603,8 +603,8 @@ rec { inherit config options; default = {dev}: { fs.${config.mountpoint} = '' - if ! findmnt ${dev} "/mnt${config.mountpoint}" > /dev/null 2>&1; then - mount ${dev} "/mnt${config.mountpoint}" \ + if ! findmnt ${dev} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then + mount ${dev} "${rootMountPoint}${config.mountpoint}" \ -t "${config.format}" \ ${concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \ -o X-mount.mkdir @@ -1169,8 +1169,8 @@ rec { ''; fs = datasetMounts.fs // optionalAttrs (!isNull config.mountpoint) { ${config.mountpoint} = '' - if ! findmnt ${config.name} "/mnt${config.mountpoint}" > /dev/null 2>&1; then - mount ${config.name} "/mnt${config.mountpoint}" \ + if ! findmnt ${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then + mount ${config.name} "${rootMountPoint}${config.mountpoint}" \ ${optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \ ${concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \ -o X-mount.mkdir \ @@ -1273,8 +1273,8 @@ rec { default = {zpool}: optionalAttrs (config.zfs_type == "volume" && !isNull config.content) (config.content._mount {dev = "/dev/zvol/${zpool}/${config.name}";}) // optionalAttrs (config.zfs_type == "filesystem" && config.options.mountpoint or "" != "none") { fs.${config.mountpoint} = '' - if ! findmnt ${zpool}/${config.name} "/mnt${config.mountpoint}" > /dev/null 2>&1; then - mount ${zpool}/${config.name} "/mnt${config.mountpoint}" \ + if ! findmnt ${zpool}/${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then + mount ${zpool}/${config.name} "${rootMountPoint}${config.mountpoint}" \ -o X-mount.mkdir \ ${concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \ ${optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \ From 6bbccd8edd06ff5c65e28c9be63f35224b7feaa3 Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 24 Jan 2023 17:09:54 +0100 Subject: [PATCH 2/4] types: relax filename, pathname & absolute pathname --- types.nix | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/types.nix b/types.nix index e12c894..fb90399 100644 --- a/types.nix +++ b/types.nix @@ -247,25 +247,22 @@ rec { }; optionTypes = rec { - # POSIX.1‐2017, 3.281 Portable Filename filename = mkOptionType { - name = "POSIX portable filename"; - check = x: isString x && builtins.match "[0-9A-Za-z._][0-9A-Za-z._-]*" x != null; + name = "filename"; + check = x: isString x; merge = mergeOneOption; description = "A filename"; }; - # POSIX.1‐2017, 3.2 Absolute Pathname absolute-pathname = mkOptionType { - name = "POSIX absolute pathname"; + name = "absolute pathname"; check = x: isString x && substring 0 1 x == "/" && pathname.check x; merge = mergeOneOption; description = "An absolute path"; }; - # POSIX.1-2017, 3.271 Pathname pathname = mkOptionType { - name = "POSIX pathname"; + name = "pathname"; check = x: let # The filter is used to normalize paths, i.e. to remove duplicated and From 08dc4d06da48ccdd17eb58b703a8934a11b41f27 Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 24 Jan 2023 17:16:09 +0100 Subject: [PATCH 3/4] disk-deactivate: follow by-id/by-uuid links to real device --- disk-deactivate/disk-deactivate | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/disk-deactivate/disk-deactivate b/disk-deactivate/disk-deactivate index c569690..6f65ad8 100755 --- a/disk-deactivate/disk-deactivate +++ b/disk-deactivate/disk-deactivate @@ -1,6 +1,6 @@ #!/bin/sh set -efux # dependencies: jq util-linux lvm2 mdadm zfs -disk=$1 +disk=$(realpath "$1") lsblk --output-all --json | jq -r --arg disk_to_clear "$disk" -f "$(dirname "$0")/disk-deactivate.jq" From f49536f0042084184bf0aabbff84c708a0e22041 Mon Sep 17 00:00:00 2001 From: lassulus Date: Sat, 28 Jan 2023 16:52:04 +0100 Subject: [PATCH 4/4] types.table: get device mountOptions the correct way --- types.nix | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/types.nix b/types.nix index fb90399..0d759e0 100644 --- a/types.nix +++ b/types.nix @@ -211,7 +211,7 @@ rec { in '' set -efux # first create the necessary devices - ${concatMapStrings (dev: ((attrByPath (dev ++ [ "_mount" ]) "" devices) {}).dev or "") sortedDeviceList} + ${concatMapStrings (dev: ((attrByPath (dev ++ [ "_mount" ]) {} devices) {}).dev or "") sortedDeviceList} # and then mount the filesystems in alphabetical order ${concatStrings (attrValues fsMounts)} @@ -680,9 +680,7 @@ rec { let partMounts = diskoLib.deepMergeMap (partition: partition._mount {inherit dev;}) config.partitions; in { - dev = '' - ${concatMapStrings (x: x.dev or "") (attrValues partMounts)} - ''; + dev = partMounts.dev or ""; fs = partMounts.fs or {}; }; };