Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos: Create persistent storage directories and their parents #193

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading