Add impermanence create option

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2025-05-30 00:13:21 +01:00
parent 3a03406b99
commit ba74461ed8
10 changed files with 71 additions and 50 deletions

View File

@@ -1,9 +1,4 @@
{
config,
lib,
pkgs,
...
}:
{ config, pkgs, ... }:
{
imports = [ ./options.nix ];

View File

@@ -121,6 +121,16 @@ in
type = str;
readOnly = true;
};
create = mkOption {
type = enum [
"none"
"file"
"directory"
];
default = "none";
description = "Whether to create the file or directory in persistence if it does not exist.";
};
};
}
)
@@ -257,6 +267,7 @@ in
source=${lib.strings.escapeShellArg c._sourceRoot}
target=${lib.strings.escapeShellArg c._targetRoot}
path=${lib.strings.escapeShellArg c.path}
create=${lib.strings.escapeShellArg c.create}
${builtins.readFile ./scripts/start.sh}
'';
@@ -264,6 +275,7 @@ in
source=${lib.strings.escapeShellArg c._sourceRoot}
target=${lib.strings.escapeShellArg c._targetRoot}
path=${lib.strings.escapeShellArg c.path}
create=${lib.strings.escapeShellArg c.create}
${builtins.readFile ./scripts/stop.sh}
'';

View File

@@ -1,22 +1,49 @@
# shellcheck shell=bash
# shellcheck disable=SC2154
echo "Starting impermanence mount with source: $source, target: $target, path: $path."
echo "Starting impermanence mount with source: $source, target: $target, path: $path, create: $create"
source_current="$source"
target_current="$target"
IFS='/' read -ra path_parts <<< "$path"
unset "path_parts[-1]"
IFS='/' read -ra parts <<< "$path"
leaf="${parts[-1]}"
for part in "${path_parts[@]}"; do
source_current="$source_current/$part"
target_current="$target_current/$part"
for part in "${parts[@]}"; do
source_current+="/$part"
target_current+="/$part"
if [[ ! -d "$source_current" ]]; then
if [[ -e "$source_current" ]]; then
read -r mode owner group <<< "$(stat -c '%a %u %g' "$source_current")"
if [[ -d "$source_current" ]]; then
install -d -m "$mode" -o "$owner" -g "$group" "$target_current"
continue
fi
if [[ "$part" != "$leaf" ]]; then
echo "Error: $source_current is not a directory, persistence for $path can not be applied."
exit 1
fi
install -m "$mode" -o "$owner" -g "$group" /dev/null "$target_current"
fi
if [[ "$create" == "none" ]]; then
break
fi
read -r mode owner group <<< "$(stat -c '%a %u %g' "$source_current")"
install -d -m "$mode" -o "$owner" -g "$group" "$target_current"
if [[ -e "$target_current" ]]; then
template="$target_current"
else
template="${source_current%/*}"
fi
read -r mode owner group <<< "$(stat -c '%a %u %g' "$template")"
if [[ "$part" == "$leaf" && "$create" == "file" ]]; then
install -m "$mode" -o "$owner" -g "$group" /dev/null "$source_current"
else
install -d -m "$mode" -o "$owner" -g "$group" "$source_current"
fi
done

View File

@@ -1,7 +1,7 @@
# shellcheck shell=bash
# shellcheck disable=SC2154
echo "Stopping impermanence mount with source: $source, target: $target, path: $path."
echo "Stopping impermanence mount with source: $source, target: $target, path: $path, create: $create"
source_current="$source"
target_current="$target"