1 { config, lib, pkgs, ... }:
6 cfg = config.security.pam.mount;
8 oflRequired = cfg.logoutHup || cfg.logoutTerm || cfg.logoutKill;
10 fake_ofl = pkgs.writeShellScriptBin "fake_ofl" ''
13 ${pkgs.lsof}/bin/lsof | ${pkgs.gnugrep}/bin/grep $MNTPT | ${pkgs.gawk}/bin/awk '{print $2}' | ${pkgs.findutils}/bin/xargs ${pkgs.util-linux}/bin/kill -$SIGNAL
16 anyPamMount = any (attrByPath ["pamMount"] false) (attrValues config.security.pam.services);
22 security.pam.mount = {
27 Enable PAM mount system to mount filesystems on user login.
31 extraVolumes = mkOption {
32 type = types.listOf types.str;
35 List of volume definitions for pam_mount.
36 For more information, visit <https://pam-mount.sourceforge.net/pam_mount.conf.5.html>.
40 additionalSearchPaths = mkOption {
41 type = types.listOf types.package;
43 example = literalExpression "[ pkgs.bindfs ]";
45 Additional programs to include in the search path of pam_mount.
46 Useful for example if you want to use some FUSE filesystems like bindfs.
50 cryptMountOptions = mkOption {
51 type = types.listOf types.str;
53 example = literalExpression ''
57 Global mount options that apply to every crypt volume.
58 You can define volume-specific options in the volume definitions.
62 fuseMountOptions = mkOption {
63 type = types.listOf types.str;
65 example = literalExpression ''
66 [ "nodev" "nosuid" "force-user=%(USER)" "gid=%(USERGID)" "perms=0700" "chmod-deny" "chown-deny" "chgrp-deny" ]
69 Global mount options that apply to every FUSE volume.
70 You can define volume-specific options in the volume definitions.
74 debugLevel = mkOption {
79 Sets the Debug-Level. 0 disables debugging, 1 enables pam_mount tracing,
80 and 2 additionally enables tracing in mount.crypt. The default is 0.
81 For more information, visit <https://pam-mount.sourceforge.net/pam_mount.conf.5.html>.
85 logoutWait = mkOption {
89 Amount of microseconds to wait until killing remaining processes after
91 For more information, visit <https://pam-mount.sourceforge.net/pam_mount.conf.5.html>.
95 logoutHup = mkOption {
99 Kill remaining processes after logout by sending a SIGHUP.
103 logoutTerm = mkOption {
107 Kill remaining processes after logout by sending a SIGTERM.
111 logoutKill = mkOption {
115 Kill remaining processes after logout by sending a SIGKILL.
119 createMountPoints = mkOption {
123 Create mountpoints for volumes if they do not exist.
127 removeCreatedMountPoints = mkOption {
131 Remove mountpoints created by pam_mount after logout. This
132 only affects mountpoints that have been created by pam_mount
140 config = mkIf (cfg.enable || anyPamMount) {
142 environment.systemPackages = [ pkgs.pam_mount ];
143 environment.etc."security/pam_mount.conf.xml" = {
146 extraUserVolumes = filterAttrs (n: u: u.cryptHomeLuks != null || u.pamMount != {}) config.users.users;
147 mkAttr = k: v: ''${k}="${v}"'';
148 userVolumeEntry = user: let
151 path = user.cryptHomeLuks;
152 mountpoint = user.home;
155 "<volume ${concatStringsSep " " (mapAttrsToList mkAttr attrs)} />\n";
157 pkgs.writeText "pam_mount.conf.xml" ''
158 <?xml version="1.0" encoding="utf-8" ?>
159 <!DOCTYPE pam_mount SYSTEM "pam_mount.conf.xml.dtd">
160 <!-- auto generated from Nixos: modules/config/users-groups.nix -->
162 <debug enable="${toString cfg.debugLevel}" />
163 <!-- if activated, requires ofl from hxtools to be present -->
164 <logout wait="${toString cfg.logoutWait}" hup="${if cfg.logoutHup then "yes" else "no"}" term="${if cfg.logoutTerm then "yes" else "no"}" kill="${if cfg.logoutKill then "yes" else "no"}" />
165 <!-- set PATH variable for pam_mount module -->
166 <path>${makeBinPath ([ pkgs.util-linux ] ++ cfg.additionalSearchPaths)}</path>
167 <!-- create mount point if not present -->
168 <mkmountpoint enable="${if cfg.createMountPoints then "1" else "0"}" remove="${if cfg.removeCreatedMountPoints then "true" else "false"}" />
169 <!-- specify the binaries to be called -->
170 <!-- the comma in front of the options is necessary for empty options -->
171 <fusemount>${pkgs.fuse}/bin/mount.fuse %(VOLUME) %(MNTPT) -o ,${concatStringsSep "," (cfg.fuseMountOptions ++ [ "%(OPTIONS)" ])}'</fusemount>
172 <fuseumount>${pkgs.fuse}/bin/fusermount -u %(MNTPT)</fuseumount>
173 <!-- the comma in front of the options is necessary for empty options -->
174 <cryptmount>${pkgs.pam_mount}/bin/mount.crypt -o ,${concatStringsSep "," (cfg.cryptMountOptions ++ [ "%(OPTIONS)" ])} %(VOLUME) %(MNTPT)</cryptmount>
175 <cryptumount>${pkgs.pam_mount}/bin/umount.crypt %(MNTPT)</cryptumount>
176 <pmvarrun>${pkgs.pam_mount}/bin/pmvarrun -u %(USER) -o %(OPERATION)</pmvarrun>
177 ${optionalString oflRequired "<ofl>${fake_ofl}/bin/fake_ofl %(SIGNAL) %(MNTPT)</ofl>"}
178 ${concatStrings (map userVolumeEntry (attrValues extraUserVolumes))}
179 ${concatStringsSep "\n" cfg.extraVolumes}