improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
/*
2023-06-16 02:14:51 +02:00
# Additions to `fileSystems`
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
2023-06-16 02:14:51 +02:00
Currently, this just adds `preMountCommands` .
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
## Implementation
```nix
#*/# end of MarkDown, beginning of NixOS module:
2023-06-16 02:14:51 +02:00
dirname: inputs: moduleArgs@{ config, pkgs, lib, utils, ... }: let lib = inputs.self.lib.__internal__; in let
inherit (inputs.config.rename) preMountCommands;
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
in {
options = {
fileSystems = lib.mkOption { type = lib.types.attrsOf (lib.types.submodule [ { options = {
2023-06-16 02:14:51 +02:00
${preMountCommands} = lib.mkOption { description = ''
2023-09-01 17:01:13 +02:00
Commands to be run as root every time before mounting this filesystem **via systemd** , but after all its dependents were mounted.
2023-05-02 02:13:24 +02:00
This does not order itself before or after `systemd-fsck@''${utils.escapeSystemdPath device}.service` .
2023-06-16 02:14:51 +02:00
This is not implemented for mounts in the initrd (those that are `neededForBoot` ) yet.
2023-09-01 17:01:13 +02:00
Note that if a symlink exists at a mount point when systemd's fstab-generator runs, it will read/resolve the symlink and use the link's target as the mount point, resulting in mismatching unit names for that mount, effectively disabling its `.${preMountCommands}` .
This does not (apparently and unfortunately) run when mounting via the `mount` command (and probably not with the `mount` system call either).
2023-05-02 02:13:24 +02:00
''; type = lib.types.lines; default = ""; };
2023-09-01 17:01:13 +02:00
#Also , trying to create the "device" of a "nofail" mount will not work with `mount` , as it will not even attempt to mount anything (and thus not run the `.${preMountCommands}` ) if the "device" is missing.
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
}; } ]);
}; };
config = let
in ({
2023-06-16 02:14:51 +02:00
assertions = lib.mapAttrsToList (name: fs: {
assertion = (fs.${preMountCommands} == "") || (!utils.fsNeededForBoot fs);
message = ''The filesystem "${name}" has `.${preMountCommands}` but is also (possibly implicitly) `.neededForBoot` . This is not currently supported.'';
}) config.fileSystems;
2022-12-27 15:37:27 +01:00
# The implementation is derived from the "mkfs-${device'}" service in nixpkgs.
2023-06-16 02:14:51 +02:00
systemd.services = lib.fun.mapMergeUnique (_: args@{ mountPoint, device, depends, ... }: if (args.${preMountCommands} != "") then let
isDevice = lib.fun.startsWith "/dev/" device;
2022-12-27 15:37:27 +01:00
mountPoint' = utils.escapeSystemdPath mountPoint;
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
device' = utils.escapeSystemdPath device;
2022-12-27 15:37:27 +01:00
in { "pre-mount-${mountPoint'}" = {
description = "Prepare mounting ${device} at ${mountPoint}";
wantedBy = [ "${mountPoint'}.mount" ]; before = [ "${mountPoint'}.mount" ];
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
requires = lib.optional isDevice "${device'}.device"; after = lib.optional isDevice "${device'}.device";
2022-12-27 15:37:27 +01:00
unitConfig.RequiresMountsFor = depends ++ [ (builtins.dirOf device) (builtins.dirOf mountPoint) ];
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
unitConfig.DefaultDependencies = false;
2023-06-16 02:14:51 +02:00
serviceConfig.Type = "oneshot"; script = args.${preMountCommands};
improve installation, add support for:
ZFS, encryption (keys, keystore, LUKS), bootFS, ephemeral root (tmpfs, ZFS, F2FS, ...), testing in qemu, options & debugging, ... and many small things
2022-05-31 03:41:28 +02:00
}; } else { }) config.fileSystems;
});
}