From 23c70023685bfec26d8c3cbb1451259ce163b105 Mon Sep 17 00:00:00 2001 From: magic_rb Date: Wed, 18 Oct 2023 14:30:17 +0200 Subject: [PATCH] Mount old versions of home on omen Signed-off-by: magic_rb --- nixos/systems/omen/impermenance.nix | 16 ++++++++++++++++ overlays/rolling_datasets/bin/mount_datasets | 18 ++++++++++++++++++ overlays/rolling_datasets/bin/roll_dataset | 2 ++ overlays/rolling_datasets/default.nix | 6 +++++- .../functions/get_old_datasets | 2 +- 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 overlays/rolling_datasets/bin/mount_datasets diff --git a/nixos/systems/omen/impermenance.nix b/nixos/systems/omen/impermenance.nix index b806093..5862695 100644 --- a/nixos/systems/omen/impermenance.nix +++ b/nixos/systems/omen/impermenance.nix @@ -85,6 +85,22 @@ ''; }; + systemd.services."mount-old-homes" = { + description = "Mount old versions of home dataset"; + + wantedBy = [ "local-fs.target" ]; + after = [ "local-fs.target" ]; + + script = '' + ${pkgs.rolling_datasets}/bin/mount_datasets "omen-ssd/ephemeral/home" "/home/.old" + ''; + + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = "yes"; + TimeoutStartSec = "5m"; + }; + }; systemd.services."home-manager-main-pre.service" = { description = "Home Manager environment prestart for main"; diff --git a/overlays/rolling_datasets/bin/mount_datasets b/overlays/rolling_datasets/bin/mount_datasets new file mode 100644 index 0000000..1044465 --- /dev/null +++ b/overlays/rolling_datasets/bin/mount_datasets @@ -0,0 +1,18 @@ +# -*- mode: shell-script -*- + +source @out@/share/functions/get_old_datasets + +export "PATH=$PATH:@runtimePath@" + +if [[ "$#" != 2 ]] ; then + echo "This script requires precisely 2 arguments!" + exit 1 +fi + +dataset="$1" +mountpoint="$2" + +while IFS="\n" read gen +do + mountpoint -q "$mountpoint/$gen" || mount -o x-mount.mkdir -v -t zfs "$dataset/$gen" "$mountpoint/$gen" +done <<< "$(get_old_datasets "$dataset")" diff --git a/overlays/rolling_datasets/bin/roll_dataset b/overlays/rolling_datasets/bin/roll_dataset index d117df0..2ba7cfc 100644 --- a/overlays/rolling_datasets/bin/roll_dataset +++ b/overlays/rolling_datasets/bin/roll_dataset @@ -2,6 +2,8 @@ source @out@/share/functions/get_old_datasets +export "PATH=$PATH:@runtimePath@" + if [[ "$#" != 3 ]] ; then echo "This script requires precisely 2 arguments!" exit 1 diff --git a/overlays/rolling_datasets/default.nix b/overlays/rolling_datasets/default.nix index 60017fd..aded02a 100644 --- a/overlays/rolling_datasets/default.nix +++ b/overlays/rolling_datasets/default.nix @@ -53,9 +53,13 @@ } '' mkdir -p $out/bin mkdir -p $out/share/functions + export runtimeShell=${prev.runtimeShell} \ + runtimePath=${prev.lib.makeBinPath (with prev; [ utillinux zfs ])} for binary in ${./bin}/* ; do - substituteAll "$binary" "$out/bin/$(basename "$binary")" + output_binary="$out/bin/$(basename "$binary")" + substituteAll "$binary" "$output_binary" + chmod +x "$output_binary" done for function in ${./functions}/* ; do diff --git a/overlays/rolling_datasets/functions/get_old_datasets b/overlays/rolling_datasets/functions/get_old_datasets index 836abea..64ad3d5 100644 --- a/overlays/rolling_datasets/functions/get_old_datasets +++ b/overlays/rolling_datasets/functions/get_old_datasets @@ -2,5 +2,5 @@ function get_old_datasets() { dataset="$1" - zfs list "$dataset" -t filesystem -r -H -o name | xargs -I {} ${final.runtimeShell} -c 'echo "$1" | rev | cut -f 1 -d "/" | rev' sh {} | grep -v "$dataset" + zfs list "$dataset" -t filesystem -r -H -o name | xargs -I {} @runtimeShell@ -c 'echo "$1" | rev | cut -f 1 -d "/" | rev' sh {} | grep -v "$(basename "$dataset")" }