Skip to content

Commit

Permalink
nixos: Create persistent storage directories and their parents
Browse files Browse the repository at this point in the history
If they're not filesystem roots, they won't exist when deploying to a
new system and would otherwise have to be manually created.
  • Loading branch information
talyz committed Oct 16, 2024
1 parent d0b38e5 commit dff77f7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
12 changes: 7 additions & 5 deletions create-directories.bash
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ fi
# check that the source exists and warn the user if it doesn't, then
# create them with the specified permissions
realSource="$(realpath -m "$sourceBase$target")"
if [[ ! -d "$realSource" ]]; then
if [[ ! -d $realSource ]]; then
printf "Warning: Source directory '%s' does not exist; it will be created for you with the following permissions: owner: '%s:%s', mode: '%s'.\n" "$realSource" "$user" "$group" "$mode"
mkdir --mode="$mode" "$realSource"
chown "$user:$group" "$realSource"
fi

[[ -d "$target" ]] || mkdir "$target"
if [[ $sourceBase ]]; then
[[ -d $target ]] || mkdir "$target"

# synchronize perms between source and target
chown --reference="$realSource" "$target"
chmod --reference="$realSource" "$target"
# synchronize perms between source and target
chown --reference="$realSource" "$target"
chmod --reference="$realSource" "$target"
fi
49 changes: 38 additions & 11 deletions nixos.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ let
patchShebangs $out
'';

defaultPerms = {
mode = "0755";
user = "root";
group = "root";
};

# Create fileSystems bind mount entry.
mkBindMountNameValuePair = { dirPath, persistentStoragePath, hideMount, ... }: {
name = concatPaths [ "/" dirPath ];
Expand Down Expand Up @@ -95,11 +101,6 @@ in
submodule (
{ name, config, ... }:
let
defaultPerms = {
mode = "0755";
user = "root";
group = "root";
};
commonOpts = {
options = {
persistentStoragePath = mkOption {
Expand Down Expand Up @@ -582,11 +583,6 @@ in
foldl'
(state: dir:
let
defaultPerms = {
mode = "0755";
user = "root";
group = "root";
};
homeDir = {
directory = dir.home;
dirPath = dir.home;
Expand All @@ -609,6 +605,29 @@ in
[ ]
explicitDirs;

# Persistent storage directories. These need to be created
# unless they're at the root of a filesystem.
persistentStorageDirs =
foldl'
(state: dir:
let
persistentStorageDir = {
directory = dir.persistentStoragePath;
dirPath = dir.persistentStoragePath;
persistentStoragePath = "";
home = null;
inherit (dir) defaultPerms enableDebugging;
inherit (dir.defaultPerms) user group mode;
};
in
if dir.home == null && !(elem persistentStorageDir state) then
state ++ [ persistentStorageDir ]
else
state
)
[ ]
(explicitDirs ++ homeDirs);

# Generate entries for all parent directories of the
# argument directories, listed in the order they need to
# be created. The parent directories are assigned default
Expand All @@ -635,6 +654,8 @@ in
in
unique (flatten (map mkParents dirs));

persistentStorageDirParents = mkParentDirs persistentStorageDirs;

# Parent directories of home folders. This is usually only
# /home, unless the user's home is in a non-standard
# location.
Expand All @@ -644,7 +665,13 @@ in
parentDirs = mkParentDirs explicitDirs;

# All directories in the order they should be created.
allDirs = homeDirParents ++ homeDirs ++ parentDirs ++ explicitDirs;
allDirs =
persistentStorageDirParents
++ persistentStorageDirs
++ homeDirParents
++ homeDirs
++ parentDirs
++ explicitDirs;
in
pkgs.writeShellScript "impermanence-run-create-directories" ''
_status=0
Expand Down

0 comments on commit dff77f7

Please sign in to comment.