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: allow files to be symlinked anywhere #8

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 39 additions & 32 deletions nixos.nix
Expand Up @@ -39,30 +39,6 @@ in
};

config = {
environment.etc =
let
link = file:
pkgs.runCommand
"${replaceStrings [ "/" "." " " ] [ "-" "" "" ] file}"
{ }
"ln -s '${file}' $out";

# Create environment.etc link entry.
mkLinkNameValuePair = persistentStoragePath: file: {
name = removePrefix "/etc/" file;
value = { source = link (concatPaths [ persistentStoragePath file ]); };
};

# Create all environment.etc link entries for a specific
# persistent storage path.
mkLinksToPersistentStorage = persistentStoragePath:
listToAttrs (map
(mkLinkNameValuePair persistentStoragePath)
cfg.${persistentStoragePath}.files
);
in
foldl' recursiveUpdate { } (map mkLinksToPersistentStorage persistentStoragePaths);

fileSystems =
let
# Create fileSystems bind mount entry.
Expand Down Expand Up @@ -150,17 +126,48 @@ in
)
'';

# Create a symlink to persistent storage, using mkDirWithPerms to
# correctly replicate the directory structure above the symlink
mkFileSymLink = persistentStoragePath: file: ''
# replicate the directory structure of ${file}
${mkDirWithPerms persistentStoragePath (dirOf file)}

# The base path for the state directory
# e.g. /state, /persist, etc.
sourceBase="${persistentStoragePath}"
sourceBase="''${sourceBase%/}"

# The path of the file in the ephemeral fs
# e.g. /etc/ssh/ssh_host_rsa_key
targetPath="${file}"

# The path of the file in the state directory
# e.g. /state/etc/ssh/ssh_host_rsa_key
sourcePath="$sourceBase$targetPath"

# Link the source file to the target
ln -s "$sourcePath" "$targetPath"
'';

# Build an activation script which creates all persistent
# storage directories we want to bind mount.
mkDirCreationScriptForPath = persistentStoragePath:
# storage directories for bind mounting, as well as all file symlinks.
mkActivationScript = persistentStoragePath:
nameValuePair
"createDirsIn-${replaceStrings [ "/" "." " " ] [ "-" "" "" ] persistentStoragePath}"
(noDepEntry (concatMapStrings
(mkDirWithPerms persistentStoragePath)
cfg.${persistentStoragePath}.directories
));
"createDirsAndLinksIn-${replaceStrings [ "/" "." " " ] [ "-" "" "" ] persistentStoragePath}"
(noDepEntry (concatStrings [
# Create directories for bind mounts
(concatMapStrings
(mkDirWithPerms persistentStoragePath)
cfg.${persistentStoragePath}.directories
)
# Create symlinks
(concatMapStrings
(mkFileSymLink persistentStoragePath)
cfg.${persistentStoragePath}.files
)
]));
in
listToAttrs (map mkDirCreationScriptForPath persistentStoragePaths);
listToAttrs (map mkActivationScript persistentStoragePaths);

assertions =
let
Expand Down