Skip to content

Commit

Permalink
refactor file & symlink
Browse files Browse the repository at this point in the history
The new system works by adding a file attribute (user.nix-minecraft-managed) to managed files and symlinks. This makes it possible to correctly cleanup managed files (when stopping, restarting, reloading).

I've also added support for directories (Infinidoge#73), and ensured only non-binary files are substituted with env vars (Infinidoge#70)
  • Loading branch information
Misterio77 committed Dec 26, 2024
1 parent 2bc6a3c commit 20ce627
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions modules/minecraft-servers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ let
"ini" = ini { };
};

isManaged = file: ''
[ "$(${pkgs.attr}/bin/getfattr --no-dereference -n user.nix-minecraft-managed --only-values "${file}" 2>/dev/null)" = "1" ]
'';
markManaged = file: ''
${pkgs.attr}/bin/setfattr --no-dereference -n user.nix-minecraft-managed -v 1 "${file}"
'';

configType = types.submodule {
options = {
format = mkOption {
Expand Down Expand Up @@ -604,37 +611,48 @@ in

ExecStartPre =
let
clean = file: ''
# If destination already exists
if [[ -e "${file}" ]]; then
# If it's managed by us, delete it. Else, backup it.
if ${isManaged file}; then
rm -rf "${n}"
else
echo "${file} already exists, moving"
mv "${file}" "${file}.bak"
fi
fi
'';
mkSymlinks = concatStringsSep "\n"
(mapAttrsToList
(n: v: ''
if [[ -L "${n}" ]]; then
unlink "${n}"
elif [[ -e "${n}" ]]; then
echo "${n} already exists, moving"
mv "${n}" "${n}.bak"
fi
${clean n}
mkdir -p "$(dirname "${n}")"
ln -sf "${v}" "${n}"
${markManaged n}
'')
symlinks);

mkFiles = concatStringsSep "\n"
(mapAttrsToList
(n: v: ''
if [[ -L "${n}" ]]; then
unlink "${n}"
elif ${pkgs.diffutils}/bin/cmp -s "${n}" "${v}"; then
rm "${n}"
elif [[ -e "${n}" ]]; then
echo "${n} already exists, moving"
mv "${n}" "${n}.bak"
fi
${clean n}
mkdir -p "$(dirname "${n}")"
${pkgs.gawk}/bin/awk '{
for(varname in ENVIRON)
gsub("@"varname"@", ENVIRON[varname])
print
}' "${v}" > "${n}"
# If it's not a binary, substitute env vars. Else, copy it normally
if file --mime-encoding "${v}" | grep -v '\bbinary$'; then
${pkgs.gawk}/bin/awk '{
for(varname in ENVIRON)
gsub("@"varname"@", ENVIRON[varname])
print
}' "${v}" > "${n}"
else
cp -r --dereference "${v}" -T "${n}"
fi
${markManaged n}
'')
files);
in
Expand Down Expand Up @@ -674,16 +692,15 @@ in

ExecStopPost =
let
rmSymlinks = concatStringsSep "\n"
(mapAttrsToList (n: v: "unlink \"${n}\"") symlinks);
rmFiles = concatStringsSep "\n"
(mapAttrsToList (n: v: "rm -f \"${n}\"") files);
# Clean all managed files/symlinks
cleanup = ''
${pkgs.findutils}/bin/find . -exec sh -c '${isManaged}' find-sh {} \; -exec rm -rf "{}" \;
'';
in
getExe (pkgs.writeShellApplication {
name = "minecraft-server-${name}-stop-post";
text = ''
${rmSymlinks}
${rmFiles}
${cleanup}
${conf.extraStopPost}
'';
});
Expand Down

0 comments on commit 20ce627

Please sign in to comment.