From c9c2fa9d3a33e566079add842853df8c1f3dd5c3 Mon Sep 17 00:00:00 2001 From: lassulus Date: Sun, 2 Jul 2023 00:17:48 +0200 Subject: [PATCH] lib: better formatting of generated shell output --- example/complex.nix | 56 +++++++++++++++++----------------------- lib/default.nix | 22 +++++++++++++--- lib/types/filesystem.nix | 2 +- lib/types/gpt.nix | 3 ++- lib/types/luks.nix | 4 ++- lib/types/lvm_pv.nix | 2 +- lib/types/lvm_vg.nix | 2 +- lib/types/mdadm.nix | 3 ++- lib/types/mdraid.nix | 2 +- lib/types/table.nix | 6 +++-- lib/types/zfs.nix | 2 +- lib/types/zfs_fs.nix | 10 +++---- lib/types/zfs_volume.nix | 6 +++-- lib/types/zpool.nix | 12 ++++----- 14 files changed, 72 insertions(+), 60 deletions(-) diff --git a/example/complex.nix b/example/complex.nix index 501de2a..7a3d924 100644 --- a/example/complex.nix +++ b/example/complex.nix @@ -5,35 +5,29 @@ type = "disk"; device = builtins.elemAt disks 0; content = { - type = "table"; - format = "gpt"; - partitions = [ - { - name = "ESP"; + type = "gpt"; + partitions = { + ESP = { start = "1MiB"; end = "128MiB"; - fs-type = "fat32"; - bootable = true; content = { type = "filesystem"; format = "vfat"; mountpoint = "/boot"; }; - } - ]; + }; + }; }; }; disk1 = { type = "disk"; device = builtins.elemAt disks 1; content = { - type = "table"; - format = "gpt"; - partitions = [ - { + type = "gpt"; + partitions = { + luks = { start = "1M"; - end = "100%"; - name = "luks"; + size = "100%"; content = { type = "luks"; name = "crypted1"; @@ -46,21 +40,19 @@ vg = "pool"; }; }; - } - ]; + }; + }; }; }; disk2 = { type = "disk"; device = builtins.elemAt disks 2; content = { - type = "table"; - format = "gpt"; - partitions = [ - { + type = "gpt"; + partitions = { + luks = { start = "1M"; - end = "100%"; - name = "luks"; + size = "100%"; content = { type = "luks"; name = "crypted2"; @@ -73,8 +65,8 @@ vg = "pool"; }; }; - } - ]; + }; + }; }; }; }; @@ -83,20 +75,18 @@ type = "mdadm"; level = 1; content = { - type = "table"; - format = "gpt"; - partitions = [ - { - name = "bla"; + type = "gpt"; + partitions = { + bla = { start = "1MiB"; - end = "100%"; + size = "100%"; content = { type = "filesystem"; format = "ext4"; mountpoint = "/ext4_mdadm_lvm"; }; - } - ]; + }; + }; }; }; }; diff --git a/lib/default.nix b/lib/default.nix index bb51e32..75e35ae 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -98,6 +98,17 @@ let in iter 1 list; + + /* indent takes a multiline string and indents it by 2 spaces starting on the second line + + indent :: str -> str + + Example: + indent "test\nbla" + => "test\n bla" + */ + indent = replaceStrings ["\n"] ["\n "]; + /* A nix option type representing a json datastructure, vendored from nixpkgs to avoid dependency on pkgs */ jsonType = let @@ -203,10 +214,10 @@ let readOnly = true; type = lib.types.str; default = '' - ( # ${config.type} ${concatMapStringsSep " " (n: toString (config.${n} or "")) ["name" "device" "format" "mountpoint"]} - ${diskoLib.defineHookVariables { inherit config options; }} + ( # ${config.type} ${concatMapStringsSep " " (n: toString (config.${n} or "")) ["name" "device" "format" "mountpoint"]} # + ${diskoLib.indent (diskoLib.defineHookVariables { inherit config options; })} ${config.preCreateHook} - ${attrs.default} + ${diskoLib.indent attrs.default} ${config.postCreateHook} ) ''; @@ -228,7 +239,10 @@ let */ writeCheckedBash = { pkgs, checked ? false, noDeps ? false }: pkgs.writers.makeScriptWriter { interpreter = if noDeps then "/usr/bin/env bash" else "${pkgs.bash}/bin/bash"; - check = lib.optionalString checked "${pkgs.shellcheck}/bin/shellcheck -e SC2034"; + check = lib.optionalString checked (pkgs.writeScript "check" '' + set -efu + ${pkgs.shellcheck}/bin/shellcheck -e SC2034 "$1" + ''); }; diff --git a/lib/types/filesystem.nix b/lib/types/filesystem.nix index 52e0f25..6f4f6da 100644 --- a/lib/types/filesystem.nix +++ b/lib/types/filesystem.nix @@ -53,7 +53,7 @@ inherit config options; default = lib.optionalAttrs (config.mountpoint != null) { fs.${config.mountpoint} = '' - if ! findmnt ${config.device} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then + if ! findmnt ${config.device} "${rootMountPoint}${config.mountpoint}" >/dev/null 2>&1; then mount ${config.device} "${rootMountPoint}${config.mountpoint}" \ -t "${config.format}" \ ${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \ diff --git a/lib/types/gpt.nix b/lib/types/gpt.nix index 3874bbb..fed2f01 100644 --- a/lib/types/gpt.nix +++ b/lib/types/gpt.nix @@ -104,7 +104,8 @@ in --typecode=${toString partition._index}:${partition.type} \ ${config.device} # ensure /dev/disk/by-path/..-partN exists before continuing - udevadm trigger --subsystem-match=block; udevadm settle + udevadm trigger --subsystem-match=block + udevadm settle ${lib.optionalString (partition.content != null) partition.content._create} '') sortedPartitions)} diff --git a/lib/types/luks.nix b/lib/types/luks.nix index 713afef..786de43 100644 --- a/lib/types/luks.nix +++ b/lib/types/luks.nix @@ -55,7 +55,9 @@ inherit config options; default = '' cryptsetup -q luksFormat ${config.device} ${diskoLib.maybeStr config.keyFile} ${toString config.extraFormatArgs} - cryptsetup luksOpen ${config.device} ${config.name} ${toString config.extraOpenArgs} ${lib.optionalString (config.keyFile != null) "--key-file ${config.keyFile}"} + cryptsetup luksOpen ${config.device} ${config.name} \ + ${toString config.extraOpenArgs} \ + ${lib.optionalString (config.keyFile != null) "--key-file ${config.keyFile}"} ${lib.optionalString (config.content != null) config.content._create} ''; }; diff --git a/lib/types/lvm_pv.nix b/lib/types/lvm_pv.nix index 067fff7..4b6cd68 100644 --- a/lib/types/lvm_pv.nix +++ b/lib/types/lvm_pv.nix @@ -32,7 +32,7 @@ inherit config options; default = '' pvcreate ${config.device} - echo "${config.device}" >> "$disko_devices_dir"/lvm_${config.vg} + echo "${config.device}" >>"$disko_devices_dir"/lvm_${config.vg} ''; }; _mount = diskoLib.mkMountOption { diff --git a/lib/types/lvm_vg.nix b/lib/types/lvm_vg.nix index 55ec33e..83397fb 100644 --- a/lib/types/lvm_vg.nix +++ b/lib/types/lvm_vg.nix @@ -60,7 +60,7 @@ '' readarray -t lvm_devices < <(cat "$disko_devices_dir"/lvm_${config.name}) vgcreate ${config.name} \ - "''${lvm_devices[@]}" + "''${lvm_devices[@]}" ${lib.concatMapStrings (lv: '' lvcreate \ --yes \ diff --git a/lib/types/mdadm.nix b/lib/types/mdadm.nix index 2392db3..500f0df 100644 --- a/lib/types/mdadm.nix +++ b/lib/types/mdadm.nix @@ -42,7 +42,8 @@ --force \ --homehost=any \ "''${disk_devices[@]}" - udevadm trigger --subsystem-match=block; udevadm settle + udevadm trigger --subsystem-match=block + udevadm settle ${lib.optionalString (config.content != null) config.content._create} ''; }; diff --git a/lib/types/mdraid.nix b/lib/types/mdraid.nix index 5e8b135..75f5d22 100644 --- a/lib/types/mdraid.nix +++ b/lib/types/mdraid.nix @@ -32,7 +32,7 @@ _create = diskoLib.mkCreateOption { inherit config options; default = '' - echo "${config.device}" >> "$disko_devices_dir"/raid_${config.name} + echo "${config.device}" >>"$disko_devices_dir"/raid_${config.name} ''; }; _mount = diskoLib.mkMountOption { diff --git a/lib/types/table.nix b/lib/types/table.nix index 9e5d9c1..8449cbd 100644 --- a/lib/types/table.nix +++ b/lib/types/table.nix @@ -91,7 +91,8 @@ parted -s ${config.device} -- mkpart ${partition.part-type} ${diskoLib.maybeStr partition.fs-type} ${diskoLib.maybeStr partition.fs-type} ${partition.start} ${partition.end} ''} # ensure /dev/disk/by-path/..-partN exists before continuing - udevadm trigger --subsystem-match=block; udevadm settle + udevadm trigger --subsystem-match=block + udevadm settle ${lib.optionalString partition.bootable '' parted -s ${config.device} -- set ${toString partition._index} boot on ''} @@ -99,7 +100,8 @@ parted -s ${config.device} -- set ${toString partition._index} ${flag} on '') partition.flags} # ensure further operations can detect new partitions - udevadm trigger --subsystem-match=block; udevadm settle + udevadm trigger --subsystem-match=block + udevadm settle ${lib.optionalString (partition.content != null) partition.content._create} '') config.partitions)} ''; diff --git a/lib/types/zfs.nix b/lib/types/zfs.nix index bf7ea18..9aa8577 100644 --- a/lib/types/zfs.nix +++ b/lib/types/zfs.nix @@ -31,7 +31,7 @@ _create = diskoLib.mkCreateOption { inherit config options; default = '' - echo "${config.device}" >> "$disko_devices_dir"/zfs_${config.pool} + echo "${config.device}" >>"$disko_devices_dir"/zfs_${config.pool} ''; }; _mount = diskoLib.mkMountOption { diff --git a/lib/types/zfs_fs.nix b/lib/types/zfs_fs.nix index 7d84a85..ed70c31 100644 --- a/lib/types/zfs_fs.nix +++ b/lib/types/zfs_fs.nix @@ -56,12 +56,12 @@ default = lib.optionalAttrs (config.options.mountpoint or "" != "none") { fs.${config.mountpoint} = '' - if ! findmnt ${config._parent.name}/${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then + if ! findmnt ${config._parent.name}/${config.name} "${rootMountPoint}${config.mountpoint}" >/dev/null 2>&1; then mount ${config._parent.name}/${config.name} "${rootMountPoint}${config.mountpoint}" \ - -o X-mount.mkdir \ - ${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \ - ${lib.optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \ - -t zfs + -o X-mount.mkdir \ + ${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \ + ${lib.optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \ + -t zfs fi ''; }; diff --git a/lib/types/zfs_volume.nix b/lib/types/zfs_volume.nix index e2c508e..51f5aef 100644 --- a/lib/types/zfs_volume.nix +++ b/lib/types/zfs_volume.nix @@ -48,8 +48,10 @@ inherit config options; default = '' zfs create ${config._parent.name}/${config.name} \ - ${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} -V ${config.size} - udevadm trigger --subsystem-match=block; udevadm settle + ${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \ + -V ${config.size} + udevadm trigger --subsystem-match=block + udevadm settle ${lib.optionalString (config.content != null) config.content._create} ''; }; diff --git a/lib/types/zpool.nix b/lib/types/zpool.nix index 41c624a..18924da 100644 --- a/lib/types/zpool.nix +++ b/lib/types/zpool.nix @@ -83,18 +83,18 @@ in { dev = '' - zpool list '${config.name}' >/dev/null 2>/dev/null || \ + zpool list '${config.name}' >/dev/null 2>/dev/null || zpool import -l -R ${config.mountRoot} '${config.name}' ${lib.concatMapStrings (x: x.dev or "") (lib.attrValues datasetMounts)} ''; fs = (datasetMounts.fs or { }) // lib.optionalAttrs (config.mountpoint != null) { ${config.mountpoint} = '' - if ! findmnt ${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then + if ! findmnt ${config.name} "${rootMountPoint}${config.mountpoint}" >/dev/null 2>&1; then mount ${config.name} "${rootMountPoint}${config.mountpoint}" \ - ${lib.optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \ - ${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \ - -o X-mount.mkdir \ - -t zfs + ${lib.optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \ + ${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \ + -o X-mount.mkdir \ + -t zfs fi ''; };