Skip to content

Commit

Permalink
nixos: Don't exit with an error in the activation script
Browse files Browse the repository at this point in the history
Exiting with an error in the activation script causes the whole script
to fail and system boot thus fails with a kernel panic. Since the
point of exiting with an error is mainly to make the systemd service
fail, let's do it only when the script runs as a service.

Fixes #77
  • Loading branch information
talyz committed Feb 14, 2022
1 parent 635bcd2 commit f0d5291
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions nixos.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let
types foldl' unique noDepEntry concatMapStrings listToAttrs
escapeShellArg escapeShellArgs replaceStrings recursiveUpdate all
filter filterAttrs concatStringsSep concatMapStringsSep isString
catAttrs optional literalExpression;
catAttrs optional optionalString literalExpression;

inherit (pkgs.callPackage ./lib.nix { }) splitPath dirListToPath
concatPaths sanitizeName duplicates;
Expand All @@ -15,14 +15,16 @@ let
allPersistentStoragePaths = { directories = [ ]; files = [ ]; users = [ ]; }
// (zipAttrsWith (_name: flatten) (attrValues cfg));
inherit (allPersistentStoragePaths) files directories;
mkMountScript = mountPoint: targetFile: ''
mkMountScript = { isService ? false, mountPoint, targetFile }: ''
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
echo "A file already exists at ${mountPoint}!" >&2
'' + optionalString isService ''
exit 1
'' + ''
elif [[ -e ${targetFile} ]]; then
touch ${mountPoint}
mount -o bind ${targetFile} ${mountPoint}
Expand Down Expand Up @@ -370,7 +372,7 @@ in
RemainAfterExit = true;
ExecStart = pkgs.writeShellScript "bindOrLink-${sanitizeName targetFile}" ''
set -eu
${mkMountScript mountPoint targetFile}
${mkMountScript { isService = true; inherit mountPoint targetFile; }}
'';
ExecStop = pkgs.writeShellScript "unbindOrUnlink-${sanitizeName targetFile}" ''
set -eu
Expand Down Expand Up @@ -447,7 +449,7 @@ in
{
"persist-${sanitizeName targetFile}" = {
deps = [ "createPersistentStorageDirs" ];
text = mkMountScript mountPoint targetFile;
text = mkMountScript { inherit mountPoint targetFile; };
};
};

Expand Down

0 comments on commit f0d5291

Please sign in to comment.