2020-06-04 21:46:38 +00:00
|
|
|
{ pkgs, config, lib, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.home.persistence;
|
|
|
|
|
|
|
|
persistentStoragePaths = attrNames cfg;
|
|
|
|
|
2020-06-07 12:36:10 +00:00
|
|
|
inherit (pkgs.callPackage ./lib.nix { }) splitPath dirListToPath concatPaths sanitizeName;
|
2020-06-04 21:46:38 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
|
|
|
|
home.persistence = mkOption {
|
|
|
|
default = { };
|
2020-06-05 17:37:07 +00:00
|
|
|
type = with types; attrsOf (
|
2021-01-24 23:35:55 +00:00
|
|
|
submodule ({ name, ... }: {
|
2020-06-04 21:46:38 +00:00
|
|
|
options =
|
|
|
|
{
|
|
|
|
directories = mkOption {
|
2020-06-07 08:12:37 +00:00
|
|
|
type = with types; listOf str;
|
2020-06-04 21:46:38 +00:00
|
|
|
default = [ ];
|
2021-01-24 22:34:16 +00:00
|
|
|
example = [
|
|
|
|
"Downloads"
|
|
|
|
"Music"
|
|
|
|
"Pictures"
|
|
|
|
"Documents"
|
|
|
|
"Videos"
|
|
|
|
"VirtualBox VMs"
|
|
|
|
".gnupg"
|
|
|
|
".ssh"
|
|
|
|
".local/share/keyrings"
|
|
|
|
".local/share/direnv"
|
|
|
|
];
|
|
|
|
description = ''
|
|
|
|
A list of directories in your home directory that
|
|
|
|
you want to link to persistent storage.
|
|
|
|
'';
|
2020-06-04 21:46:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
files = mkOption {
|
2020-06-07 08:12:37 +00:00
|
|
|
type = with types; listOf str;
|
2020-06-04 21:46:38 +00:00
|
|
|
default = [ ];
|
2021-01-24 22:34:16 +00:00
|
|
|
example = [
|
|
|
|
".screenrc"
|
|
|
|
];
|
|
|
|
description = ''
|
|
|
|
A list of files in your home directory you want to
|
|
|
|
link to persistent storage.
|
|
|
|
'';
|
2020-06-04 21:46:38 +00:00
|
|
|
};
|
|
|
|
|
2021-01-24 23:35:55 +00:00
|
|
|
allowOther = mkOption {
|
|
|
|
type = with types; nullOr bool;
|
|
|
|
default = null;
|
|
|
|
example = true;
|
|
|
|
apply = x:
|
|
|
|
if x == null then
|
|
|
|
warn ''
|
|
|
|
home.persistence."${name}".allowOther not set; assuming 'false'.
|
|
|
|
See https://github.com/nix-community/impermanence#home-manager for more info.
|
|
|
|
''
|
|
|
|
false
|
|
|
|
else
|
|
|
|
x;
|
|
|
|
description = ''
|
|
|
|
Whether to allow other users, such as
|
|
|
|
<literal>root</literal>, access to files through the
|
|
|
|
bind mounted directories listed in
|
|
|
|
<literal>directories</literal>. Requires the NixOS
|
|
|
|
configuration parameter
|
|
|
|
<literal>programs.fuse.userAllowOther</literal> to
|
|
|
|
be <literal>true</literal>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-06-04 21:46:38 +00:00
|
|
|
removePrefixDirectory = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2021-01-24 22:34:16 +00:00
|
|
|
example = true;
|
|
|
|
description = ''
|
|
|
|
Note: This is mainly useful if you have a dotfiles
|
|
|
|
repo structured for use with GNU Stow; if you don't,
|
|
|
|
you can likely ignore it.
|
|
|
|
|
|
|
|
Whether to remove the first directory when linking
|
|
|
|
or mounting; e.g. for the path
|
|
|
|
<literal>"screen/.screenrc"</literal>, the
|
|
|
|
<literal>screen/</literal> is ignored for the path
|
|
|
|
linked to in your home directory.
|
|
|
|
'';
|
2020-06-04 21:46:38 +00:00
|
|
|
};
|
|
|
|
};
|
2021-01-24 23:35:55 +00:00
|
|
|
})
|
2020-06-04 21:46:38 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
home.file =
|
|
|
|
let
|
|
|
|
link = file:
|
|
|
|
pkgs.runCommand
|
2020-06-07 12:36:10 +00:00
|
|
|
"${sanitizeName file}"
|
2020-06-04 21:46:38 +00:00
|
|
|
{ }
|
|
|
|
"ln -s '${file}' $out";
|
|
|
|
|
2020-06-07 12:36:10 +00:00
|
|
|
mkLinkNameValuePair = persistentStoragePath: file: {
|
2020-06-05 17:37:07 +00:00
|
|
|
name =
|
|
|
|
if cfg.${persistentStoragePath}.removePrefixDirectory then
|
2020-06-07 12:36:10 +00:00
|
|
|
dirListToPath (tail (splitPath [ file ]))
|
2020-06-05 17:37:07 +00:00
|
|
|
else
|
2020-06-07 12:36:10 +00:00
|
|
|
file;
|
|
|
|
value = { source = link (concatPaths [ persistentStoragePath file ]); };
|
2020-06-04 21:46:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mkLinksToPersistentStorage = persistentStoragePath:
|
2020-06-05 17:37:07 +00:00
|
|
|
listToAttrs (map
|
|
|
|
(mkLinkNameValuePair persistentStoragePath)
|
2020-06-07 12:36:10 +00:00
|
|
|
(cfg.${persistentStoragePath}.files)
|
2020-06-05 17:37:07 +00:00
|
|
|
);
|
2020-06-04 21:46:38 +00:00
|
|
|
in
|
2020-06-05 17:37:07 +00:00
|
|
|
foldl' recursiveUpdate { } (map mkLinksToPersistentStorage persistentStoragePaths);
|
2020-06-04 21:46:38 +00:00
|
|
|
|
2020-06-07 12:36:10 +00:00
|
|
|
systemd.user.services =
|
|
|
|
let
|
|
|
|
mkBindMountService = persistentStoragePath: dir:
|
|
|
|
let
|
|
|
|
mountDir =
|
|
|
|
if cfg.${persistentStoragePath}.removePrefixDirectory then
|
|
|
|
dirListToPath (tail (splitPath [ dir ]))
|
|
|
|
else
|
|
|
|
dir;
|
2020-09-19 16:53:07 +00:00
|
|
|
targetDir = escapeShellArg (concatPaths [ persistentStoragePath dir ]);
|
|
|
|
mountPoint = escapeShellArg (concatPaths [ config.home.homeDirectory mountDir ]);
|
2020-06-07 12:36:10 +00:00
|
|
|
name = "bindMount-${sanitizeName targetDir}";
|
2021-01-24 22:26:22 +00:00
|
|
|
bindfsOptions = concatStringsSep "," (
|
2021-01-24 23:35:55 +00:00
|
|
|
optional (!cfg.${persistentStoragePath}.allowOther) "no-allow-other"
|
|
|
|
++ optional (versionAtLeast pkgs.bindfs.version "1.14.9") "fsname=${targetDir}"
|
|
|
|
);
|
2021-01-26 21:31:17 +00:00
|
|
|
bindfsOptionFlag = optionalString (bindfsOptions != "") (" -o " + bindfsOptions);
|
2021-01-24 23:35:55 +00:00
|
|
|
bindfs = "bindfs -f" + bindfsOptionFlag;
|
2020-06-07 12:36:10 +00:00
|
|
|
startScript = pkgs.writeShellScript name ''
|
|
|
|
set -eu
|
2020-09-26 10:57:36 +00:00
|
|
|
if ! mount | grep -F ${mountPoint}' ' && ! mount | grep -F ${mountPoint}/; then
|
2021-01-24 23:35:55 +00:00
|
|
|
${bindfs} ${targetDir} ${mountPoint}
|
2020-06-07 12:36:10 +00:00
|
|
|
else
|
|
|
|
echo "There is already an active mount at or below ${mountPoint}!" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
stopScript = pkgs.writeShellScript "unmount-${name}" ''
|
2020-09-19 16:53:07 +00:00
|
|
|
set -eu
|
2020-06-20 12:56:23 +00:00
|
|
|
triesLeft=6
|
|
|
|
while (( triesLeft > 0 )); do
|
2020-09-19 16:53:07 +00:00
|
|
|
if fusermount -u ${mountPoint}; then
|
2020-06-20 12:56:23 +00:00
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
(( triesLeft-- ))
|
|
|
|
if (( triesLeft == 0 )); then
|
|
|
|
echo "Couldn't perform regular unmount of ${mountPoint}. Attempting lazy unmount."
|
2020-09-19 16:53:07 +00:00
|
|
|
fusermount -uz ${mountPoint}
|
2020-06-20 12:56:23 +00:00
|
|
|
else
|
|
|
|
sleep 5
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2020-06-07 12:36:10 +00:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
inherit name;
|
|
|
|
value = {
|
|
|
|
Unit = {
|
|
|
|
Description = "Bind mount ${targetDir} at ${mountPoint}";
|
|
|
|
|
|
|
|
# Don't restart the unit, it could corrupt data and
|
|
|
|
# crash programs currently reading from the mount.
|
|
|
|
X-RestartIfChanged = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
Install.WantedBy = [ "default.target" ];
|
|
|
|
|
|
|
|
Service = {
|
|
|
|
ExecStart = "${startScript}";
|
|
|
|
ExecStop = "${stopScript}";
|
2021-01-27 11:13:21 +00:00
|
|
|
Environment = "PATH=${makeBinPath [ pkgs.coreutils pkgs.utillinux pkgs.gnugrep pkgs.bindfs ]}:/run/wrappers/bin";
|
2020-06-07 12:36:10 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mkBindMountServicesForPath = persistentStoragePath:
|
|
|
|
listToAttrs (map
|
|
|
|
(mkBindMountService persistentStoragePath)
|
|
|
|
cfg.${persistentStoragePath}.directories
|
|
|
|
);
|
|
|
|
in
|
|
|
|
builtins.foldl'
|
|
|
|
recursiveUpdate
|
|
|
|
{ }
|
|
|
|
(map mkBindMountServicesForPath persistentStoragePaths);
|
|
|
|
|
2020-06-04 21:46:38 +00:00
|
|
|
home.activation =
|
|
|
|
let
|
|
|
|
dag = config.lib.dag;
|
|
|
|
|
2021-01-23 13:02:36 +00:00
|
|
|
# The name of the activation script entry responsible for
|
|
|
|
# reloading systemd user services. The name was initially
|
|
|
|
# `reloadSystemD` but has been changed to `reloadSystemd`.
|
|
|
|
reloadSystemd =
|
|
|
|
if config.home.activation ? reloadSystemD then
|
|
|
|
"reloadSystemD"
|
|
|
|
else
|
|
|
|
"reloadSystemd";
|
|
|
|
|
2020-06-07 12:36:10 +00:00
|
|
|
mkBindMount = persistentStoragePath: dir:
|
2020-06-04 21:46:38 +00:00
|
|
|
let
|
2020-06-07 12:36:10 +00:00
|
|
|
mountDir =
|
|
|
|
if cfg.${persistentStoragePath}.removePrefixDirectory then
|
|
|
|
dirListToPath (tail (splitPath [ dir ]))
|
|
|
|
else
|
|
|
|
dir;
|
2020-09-19 16:53:07 +00:00
|
|
|
targetDir = escapeShellArg (concatPaths [ persistentStoragePath dir ]);
|
|
|
|
mountPoint = escapeShellArg (concatPaths [ config.home.homeDirectory mountDir ]);
|
2020-09-26 10:57:36 +00:00
|
|
|
mount = "${pkgs.utillinux}/bin/mount";
|
2021-01-24 22:26:22 +00:00
|
|
|
bindfsOptions = concatStringsSep "," (
|
2021-01-24 23:35:55 +00:00
|
|
|
optional (!cfg.${persistentStoragePath}.allowOther) "no-allow-other"
|
|
|
|
++ optional (versionAtLeast pkgs.bindfs.version "1.14.9") "fsname=${targetDir}"
|
|
|
|
);
|
2021-01-26 21:31:17 +00:00
|
|
|
bindfsOptionFlag = optionalString (bindfsOptions != "") (" -o " + bindfsOptions);
|
2021-01-24 23:35:55 +00:00
|
|
|
bindfs = "${pkgs.bindfs}/bin/bindfs" + bindfsOptionFlag;
|
2020-06-07 12:36:10 +00:00
|
|
|
systemctl = "XDG_RUNTIME_DIR=\${XDG_RUNTIME_DIR:-/run/user/$(id -u)} ${config.systemd.user.systemctlPath}";
|
2020-06-05 17:37:07 +00:00
|
|
|
in
|
|
|
|
''
|
2021-01-27 11:37:08 +00:00
|
|
|
mkdir -p ${targetDir}
|
|
|
|
mkdir -p ${mountPoint}
|
|
|
|
|
2020-09-26 10:57:36 +00:00
|
|
|
if ${mount} | grep -F ${mountPoint}' ' >/dev/null; then
|
2021-01-23 15:09:03 +00:00
|
|
|
if ! ${mount} | grep -F ${mountPoint}' ' | grep -F bindfs; then
|
2021-01-24 22:26:22 +00:00
|
|
|
if ! ${mount} | grep -F ${mountPoint}' ' | grep -F ${targetDir}' ' >/dev/null; then
|
2021-01-23 15:09:03 +00:00
|
|
|
# The target directory changed, so we need to remount
|
|
|
|
echo "remounting ${mountPoint}"
|
|
|
|
${systemctl} --user stop bindMount-${sanitizeName targetDir}
|
2021-01-26 21:31:17 +00:00
|
|
|
${bindfs} ${targetDir} ${mountPoint}
|
2021-01-23 15:09:03 +00:00
|
|
|
mountedPaths[${mountPoint}]=1
|
|
|
|
fi
|
2020-06-07 12:36:10 +00:00
|
|
|
fi
|
2020-09-26 10:57:36 +00:00
|
|
|
elif ${mount} | grep -F ${mountPoint}/ >/dev/null; then
|
2020-09-19 16:53:07 +00:00
|
|
|
echo "Something is mounted below ${mountPoint}, not creating bind mount to ${targetDir}" >&2
|
2020-06-07 12:36:10 +00:00
|
|
|
else
|
2021-01-24 22:26:22 +00:00
|
|
|
${bindfs} ${targetDir} ${mountPoint}
|
2020-09-19 16:53:07 +00:00
|
|
|
mountedPaths[${mountPoint}]=1
|
2020-06-07 12:36:10 +00:00
|
|
|
fi
|
2020-06-04 21:46:38 +00:00
|
|
|
'';
|
|
|
|
|
2020-06-07 12:36:10 +00:00
|
|
|
mkBindMountsForPath = persistentStoragePath:
|
|
|
|
concatMapStrings
|
|
|
|
(mkBindMount persistentStoragePath)
|
|
|
|
cfg.${persistentStoragePath}.directories;
|
|
|
|
|
|
|
|
mkUnmount = persistentStoragePath: dir:
|
|
|
|
let
|
|
|
|
mountDir =
|
|
|
|
if cfg.${persistentStoragePath}.removePrefixDirectory then
|
|
|
|
dirListToPath (tail (splitPath [ dir ]))
|
|
|
|
else
|
|
|
|
dir;
|
2020-09-19 16:53:07 +00:00
|
|
|
mountPoint = escapeShellArg (concatPaths [ config.home.homeDirectory mountDir ]);
|
2020-06-07 12:36:10 +00:00
|
|
|
in
|
|
|
|
''
|
2020-09-19 16:53:07 +00:00
|
|
|
if [[ -n ''${mountedPaths[${mountPoint}]+x} ]]; then
|
2020-06-20 12:56:23 +00:00
|
|
|
triesLeft=3
|
|
|
|
while (( triesLeft > 0 )); do
|
2020-09-19 16:53:07 +00:00
|
|
|
if fusermount -u ${mountPoint}; then
|
2020-06-20 12:56:23 +00:00
|
|
|
break
|
|
|
|
else
|
|
|
|
(( triesLeft-- ))
|
|
|
|
if (( triesLeft == 0 )); then
|
|
|
|
echo "Couldn't perform regular unmount of ${mountPoint}. Attempting lazy unmount."
|
2020-09-19 16:53:07 +00:00
|
|
|
fusermount -uz ${mountPoint} || true
|
2020-06-20 12:56:23 +00:00
|
|
|
else
|
|
|
|
sleep 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2020-06-07 12:36:10 +00:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
mkUnmountsForPath = persistentStoragePath:
|
|
|
|
concatMapStrings
|
|
|
|
(mkUnmount persistentStoragePath)
|
|
|
|
cfg.${persistentStoragePath}.directories;
|
|
|
|
|
2020-06-04 21:46:38 +00:00
|
|
|
in
|
2020-06-07 12:36:10 +00:00
|
|
|
mkIf (any (path: cfg.${path}.directories != [ ]) persistentStoragePaths) {
|
|
|
|
createAndMountPersistentStoragePaths =
|
|
|
|
dag.entryBefore
|
|
|
|
[ "writeBoundary" ]
|
|
|
|
''
|
|
|
|
declare -A mountedPaths
|
|
|
|
${(concatMapStrings mkBindMountsForPath persistentStoragePaths)}
|
|
|
|
'';
|
|
|
|
|
|
|
|
unmountPersistentStoragePaths =
|
|
|
|
dag.entryBefore
|
|
|
|
[ "createAndMountPersistentStoragePaths" ]
|
|
|
|
''
|
|
|
|
unmountBindMounts() {
|
|
|
|
${concatMapStrings mkUnmountsForPath persistentStoragePaths}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Run the unmount function on error to clean up stray
|
|
|
|
# bind mounts
|
|
|
|
trap "unmountBindMounts" ERR
|
|
|
|
'';
|
|
|
|
|
|
|
|
runUnmountPersistentStoragePaths =
|
|
|
|
dag.entryBefore
|
2021-01-23 13:02:36 +00:00
|
|
|
[ reloadSystemd ]
|
2020-06-07 12:36:10 +00:00
|
|
|
''
|
|
|
|
unmountBindMounts
|
|
|
|
'';
|
|
|
|
};
|
2020-06-04 21:46:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|