diff options
| -rw-r--r-- | autofs.nix | 120 | ||||
| -rw-r--r-- | bragi.nix | 1 |
2 files changed, 121 insertions, 0 deletions
diff --git a/autofs.nix b/autofs.nix new file mode 100644 index 00000000..f4a1059d --- /dev/null +++ b/autofs.nix | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | { config, lib, pkgs, ... }: | ||
| 2 | |||
| 3 | with lib; | ||
| 4 | |||
| 5 | let | ||
| 6 | |||
| 7 | cfg = config.services.autofs; | ||
| 8 | |||
| 9 | autoMaster = pkgs.writeText "auto.master" cfg.autoMaster; | ||
| 10 | |||
| 11 | in | ||
| 12 | |||
| 13 | { | ||
| 14 | |||
| 15 | ###### interface | ||
| 16 | |||
| 17 | options = { | ||
| 18 | |||
| 19 | services.autofs = { | ||
| 20 | |||
| 21 | enable = mkOption { | ||
| 22 | default = false; | ||
| 23 | description = " | ||
| 24 | Mount filesystems on demand. Unmount them automatically. | ||
| 25 | You may also be interested in afuese. | ||
| 26 | "; | ||
| 27 | }; | ||
| 28 | |||
| 29 | autoMaster = mkOption { | ||
| 30 | example = literalExample '' | ||
| 31 | autoMaster = let | ||
| 32 | mapConf = pkgs.writeText "auto" ''' | ||
| 33 | kernel -ro,soft,intr ftp.kernel.org:/pub/linux | ||
| 34 | boot -fstype=ext2 :/dev/hda1 | ||
| 35 | windoze -fstype=smbfs ://windoze/c | ||
| 36 | removable -fstype=ext2 :/dev/hdd | ||
| 37 | cd -fstype=iso9660,ro :/dev/hdc | ||
| 38 | floppy -fstype=auto :/dev/fd0 | ||
| 39 | server -rw,hard,intr / -ro myserver.me.org:/ \ | ||
| 40 | /usr myserver.me.org:/usr \ | ||
| 41 | /home myserver.me.org:/home | ||
| 42 | '''; | ||
| 43 | in ''' | ||
| 44 | /auto file:''${mapConf} | ||
| 45 | ''' | ||
| 46 | ''; | ||
| 47 | description = " | ||
| 48 | file contents of /etc/auto.master. See man auto.master | ||
| 49 | See man 5 auto.master and man 5 autofs. | ||
| 50 | "; | ||
| 51 | }; | ||
| 52 | |||
| 53 | timeout = mkOption { | ||
| 54 | default = 600; | ||
| 55 | description = "Set the global minimum timeout, in seconds, until directories are unmounted"; | ||
| 56 | }; | ||
| 57 | |||
| 58 | debug = mkOption { | ||
| 59 | default = false; | ||
| 60 | description = " | ||
| 61 | pass -d and -7 to automount and write log to /var/log/autofs | ||
| 62 | "; | ||
| 63 | }; | ||
| 64 | |||
| 65 | }; | ||
| 66 | |||
| 67 | }; | ||
| 68 | |||
| 69 | |||
| 70 | ###### implementation | ||
| 71 | |||
| 72 | config = mkIf cfg.enable { | ||
| 73 | |||
| 74 | environment.etc = singleton | ||
| 75 | { target = "auto.master"; | ||
| 76 | source = pkgs.writeText "auto.master" cfg.autoMaster; | ||
| 77 | }; | ||
| 78 | |||
| 79 | boot.kernelModules = [ "autofs4" ]; | ||
| 80 | |||
| 81 | jobs.autofs = | ||
| 82 | { description = "Filesystem automounter"; | ||
| 83 | |||
| 84 | startOn = "started network-interfaces"; | ||
| 85 | stopOn = "stopping network-interfaces"; | ||
| 86 | |||
| 87 | path = [ pkgs.nfs-utils pkgs.sshfsFuse ]; | ||
| 88 | |||
| 89 | preStop = | ||
| 90 | '' | ||
| 91 | set -e; while :; do pkill -TERM automount; sleep 1; done | ||
| 92 | ''; | ||
| 93 | |||
| 94 | # automount doesn't clean up when receiving SIGKILL. | ||
| 95 | # umount -l should unmount the directories recursively when they are no longer used | ||
| 96 | # It does, but traces are left in /etc/mtab. So unmount recursively.. | ||
| 97 | postStop = | ||
| 98 | '' | ||
| 99 | PATH=${pkgs.gnused}/bin:${pkgs.coreutils}/bin | ||
| 100 | exec &> /tmp/logss | ||
| 101 | # double quote for sed: | ||
| 102 | escapeSpaces(){ sed 's/ /\\\\040/g'; } | ||
| 103 | unescapeSpaces(){ sed 's/\\040/ /g'; } | ||
| 104 | sed -n 's@^\s*\(\([^\\ ]\|\\ \)*\)\s.*@\1@p' ${autoMaster} | sed 's/[\\]//' | while read mountPoint; do | ||
| 105 | sed -n "s@[^ ]\+\s\+\($(echo "$mountPoint"| escapeSpaces)[^ ]*\).*@\1@p" /proc/mounts | sort -r | unescapeSpaces| while read smountP; do | ||
| 106 | ${pkgs.utillinux}/bin/umount -l "$smountP" || true | ||
| 107 | done | ||
| 108 | done | ||
| 109 | ''; | ||
| 110 | |||
| 111 | script = | ||
| 112 | '' | ||
| 113 | ${if cfg.debug then "exec &> /var/log/autofs" else ""} | ||
| 114 | exec ${pkgs.autofs5}/sbin/automount ${if cfg.debug then "-d" else ""} -f -t ${builtins.toString cfg.timeout} "${autoMaster}" ${if cfg.debug then "-l7" else ""} | ||
| 115 | ''; | ||
| 116 | }; | ||
| 117 | |||
| 118 | }; | ||
| 119 | |||
| 120 | } | ||
| @@ -4,6 +4,7 @@ | |||
| 4 | imports = | 4 | imports = |
| 5 | [ | 5 | [ |
| 6 | ./bragi-hw.nix | 6 | ./bragi-hw.nix |
| 7 | ./autofs.nix | ||
| 7 | ]; | 8 | ]; |
| 8 | 9 | ||
| 9 | boot.loader.grub.enable = true; | 10 | boot.loader.grub.enable = true; |
