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

Add force option for replacing existing files #88

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
files = [
"/etc/machine-id"
{ file = "/etc/nix/id_rsa"; parentDirectory = { mode = "u=rwx,g=,o="; }; }
{ file = "/etc/shadow"; force = true; }; }
];
users.talyz = {
directories = [
Expand Down
20 changes: 15 additions & 5 deletions mount-file.bash
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ shopt -s inherit_errexit # Inherit the errexit option status in subshells.
trap 'echo Error when executing ${BASH_COMMAND} at line ${LINENO}! >&2' ERR

# Get inputs from command line arguments
if [[ "$#" != 3 ]]; then
echo "Error: 'mount-file.bash' requires *three* args." >&2
if [[ "$#" != 4 ]]; then
echo "Error: 'mount-file.bash' requires *four* args." >&2
exit 1
fi

mountPoint="$1"
targetFile="$2"
debug="$3"
force="$4"

if (( "$debug" )); then
set -o xtrace
Expand All @@ -27,12 +28,21 @@ if [[ -L "$mountPoint" && $(readlink -f "$mountPoint") == "$targetFile" ]]; then
echo "$mountPoint already links to $targetFile, ignoring"
elif mount | grep -F "$mountPoint"' ' >/dev/null && ! mount | grep -F "$mountPoint"/ >/dev/null; then
echo "mount already exists at $mountPoint, ignoring"
elif [[ -e "$mountPoint" ]]; then
elif [[ -z "$force" ]] && [[ -e "$mountPoint" ]]; then
echo "A file already exists at $mountPoint!" >&2
exit 1
elif [[ -e "$targetFile" ]]; then
touch "$mountPoint"
if [[ -f "$mountPoint" ]]; then
truncate -s 0 "$mountPoint"
else
rm -f "$mountPoint"
touch "$mountPoint"
fi
mount -o bind "$targetFile" "$mountPoint"
else
ln -s "$targetFile" "$mountPoint"
if [[ -n "$force" ]]; then
ln -sf "$targetFile" "$mountPoint"
else
ln -s "$targetFile" "$mountPoint"
fi
fi
19 changes: 16 additions & 3 deletions nixos.nix
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ in
'';
};
parentDirectory = dirPermsOpts perms;
force = mkOption {
type = bool;
default = false;
description = ''
Whether to replace the mountpoint file if it
already exists. This will not replace a
directory if present.
'';
};
};
};
dirOpts = perms: {
Expand Down Expand Up @@ -170,6 +179,7 @@ in
inherit persistentStoragePath;
file = concatPaths [ config.home file ];
parentDirectory = userDefaultPerms;
force = false;
}
else
file // {
Expand Down Expand Up @@ -259,6 +269,7 @@ in
{
inherit file persistentStoragePath;
parentDirectory = defaultPerms;
force = false;
}
else
file);
Expand Down Expand Up @@ -341,6 +352,7 @@ in
files = [
"/etc/machine-id"
{ file = "/etc/nix/id_rsa"; parentDirectory = { mode = "u=rwx,g=,o="; }; }
{ file = "/etc/shadow"; force = true; }; }
];
};
users.talyz = { ... }; # See the dedicated example
Expand All @@ -353,7 +365,7 @@ in
config = {
systemd.services =
let
mkPersistFileService = { file, persistentStoragePath, ... }:
mkPersistFileService = { file, persistentStoragePath, force, ... }:
let
targetFile = escapeShellArg (concatPaths [ persistentStoragePath file ]);
mountPoint = escapeShellArg file;
Expand All @@ -369,7 +381,7 @@ in
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${mountFile} ${mountPoint} ${targetFile} ${enableDebugging}";
ExecStart = "${mountFile} ${mountPoint} ${targetFile} ${enableDebugging} ${escapeShellArg force}";
ExecStop = pkgs.writeShellScript "unbindOrUnlink-${sanitizeName targetFile}" ''
set -eu
if [[ -L ${mountPoint} ]]; then
Expand Down Expand Up @@ -448,14 +460,15 @@ in
exit $_status
'';

mkPersistFile = { file, persistentStoragePath, ... }:
mkPersistFile = { file, persistentStoragePath, force, ... }:
let
mountPoint = file;
targetFile = concatPaths [ persistentStoragePath file ];
args = escapeShellArgs [
mountPoint
targetFile
cfg.${persistentStoragePath}.enableDebugging
force
];
in
''
Expand Down