|
| 1 | +{ |
| 2 | + config, |
| 3 | + pkgs, |
| 4 | + lib, |
| 5 | + utils, |
| 6 | + ... |
| 7 | +}: |
| 8 | +let |
| 9 | + cfg = config.services.filebrowser; |
| 10 | + inherit (lib) types; |
| 11 | + format = pkgs.formats.json { }; |
| 12 | +in |
| 13 | +{ |
| 14 | + options = { |
| 15 | + services.filebrowser = { |
| 16 | + enable = lib.mkEnableOption "FileBrowser"; |
| 17 | + |
| 18 | + package = lib.mkPackageOption pkgs "filebrowser" { }; |
| 19 | + |
| 20 | + openFirewall = lib.mkEnableOption "opening firewall ports for FileBrowser"; |
| 21 | + |
| 22 | + settings = lib.mkOption { |
| 23 | + default = { }; |
| 24 | + description = '' |
| 25 | + Settings for FileBrowser. |
| 26 | + Refer to <https://filebrowser.org/cli/filebrowser#options> for all supported values. |
| 27 | + ''; |
| 28 | + type = types.submodule { |
| 29 | + freeformType = format.type; |
| 30 | + |
| 31 | + options = { |
| 32 | + address = lib.mkOption { |
| 33 | + default = "localhost"; |
| 34 | + description = '' |
| 35 | + The address to listen on. |
| 36 | + ''; |
| 37 | + type = types.str; |
| 38 | + }; |
| 39 | + |
| 40 | + port = lib.mkOption { |
| 41 | + default = 8080; |
| 42 | + description = '' |
| 43 | + The port to listen on. |
| 44 | + ''; |
| 45 | + type = types.port; |
| 46 | + }; |
| 47 | + |
| 48 | + root = lib.mkOption { |
| 49 | + default = "/var/lib/filebrowser/data"; |
| 50 | + description = '' |
| 51 | + The directory where FileBrowser stores files. |
| 52 | + ''; |
| 53 | + type = types.path; |
| 54 | + }; |
| 55 | + |
| 56 | + database = lib.mkOption { |
| 57 | + default = "/var/lib/filebrowser/database.db"; |
| 58 | + description = '' |
| 59 | + The path to FileBrowser's Bolt database. |
| 60 | + ''; |
| 61 | + type = types.path; |
| 62 | + }; |
| 63 | + |
| 64 | + cache-dir = lib.mkOption { |
| 65 | + default = "/var/cache/filebrowser"; |
| 66 | + description = '' |
| 67 | + The directory where FileBrowser stores its cache. |
| 68 | + ''; |
| 69 | + type = types.path; |
| 70 | + readOnly = true; |
| 71 | + }; |
| 72 | + }; |
| 73 | + }; |
| 74 | + }; |
| 75 | + }; |
| 76 | + }; |
| 77 | + |
| 78 | + config = lib.mkIf cfg.enable { |
| 79 | + systemd = { |
| 80 | + services.filebrowser = { |
| 81 | + after = [ "network.target" ]; |
| 82 | + description = "FileBrowser"; |
| 83 | + wantedBy = [ "multi-user.target" ]; |
| 84 | + serviceConfig = { |
| 85 | + ExecStart = |
| 86 | + let |
| 87 | + args = [ |
| 88 | + (lib.getExe cfg.package) |
| 89 | + "--config" |
| 90 | + (format.generate "config.json" cfg.settings) |
| 91 | + ]; |
| 92 | + in |
| 93 | + utils.escapeSystemdExecArgs args; |
| 94 | + |
| 95 | + StateDirectory = "filebrowser"; |
| 96 | + CacheDirectory = "filebrowser"; |
| 97 | + WorkingDirectory = cfg.settings.root; |
| 98 | + |
| 99 | + DynamicUser = true; |
| 100 | + |
| 101 | + NoNewPrivileges = true; |
| 102 | + PrivateDevices = true; |
| 103 | + ProtectKernelTunables = true; |
| 104 | + ProtectKernelModules = true; |
| 105 | + ProtectControlGroups = true; |
| 106 | + MemoryDenyWriteExecute = true; |
| 107 | + LockPersonality = true; |
| 108 | + RestrictAddressFamilies = [ |
| 109 | + "AF_UNIX" |
| 110 | + "AF_INET" |
| 111 | + "AF_INET6" |
| 112 | + ]; |
| 113 | + DevicePolicy = "closed"; |
| 114 | + RestrictNamespaces = true; |
| 115 | + RestrictRealtime = true; |
| 116 | + RestrictSUIDSGID = true; |
| 117 | + }; |
| 118 | + }; |
| 119 | + |
| 120 | + tmpfiles.settings.filebrowser = |
| 121 | + lib.genAttrs |
| 122 | + [ |
| 123 | + cfg.settings.root |
| 124 | + (builtins.dirOf cfg.settings.database) |
| 125 | + ] |
| 126 | + (_: { |
| 127 | + d.mode = "0700"; |
| 128 | + }); |
| 129 | + }; |
| 130 | + |
| 131 | + networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.settings.port ]; |
| 132 | + }; |
| 133 | + |
| 134 | + meta.maintainers = [ |
| 135 | + lib.maintainers.lukaswrz |
| 136 | + ]; |
| 137 | +} |
0 commit comments