shirts.nix (2777B)
1 { 2 config, 3 lib, 4 pkgs, 5 inputs, 6 haravara, 7 ... 8 }: let 9 cfg = config.services.haravara.shirts; 10 11 shirts = inputs.shirts.packages.${pkgs.stdenv.hostPlatform.system}.default; 12 in { 13 options.services.haravara.shirts = { 14 enable = lib.mkEnableOption "link shortener that beams up"; 15 16 port = lib.mkOption { 17 type = lib.types.port; 18 default = 8721; 19 description = "Port for the Shirts service to listen on"; 20 }; 21 22 host = lib.mkOption { 23 type = lib.types.str; 24 default = "127.0.0.1"; 25 description = "Host address for the Shirts service to bind to"; 26 }; 27 28 baseUrl = lib.mkOption { 29 type = lib.types.str; 30 description = "Base URL for the Shirts shortener"; 31 }; 32 33 authTokenFile = lib.mkOption { 34 type = lib.types.path; 35 description = "Path to a file containing the authentication token"; 36 }; 37 38 domain = lib.mkOption { 39 type = lib.types.str; 40 description = "Domain for the Shirts instance"; 41 }; 42 43 dataDir = lib.mkOption { 44 type = lib.types.str; 45 default = "/var/lib/shirts"; 46 description = "Directory for Shirts state data"; 47 }; 48 }; 49 50 config = lib.mkIf cfg.enable { 51 systemd.services.shirts = { 52 description = "Shirts URL Shortener"; 53 wantedBy = ["multi-user.target"]; 54 after = ["network.target"]; 55 56 environment = { 57 PORT = toString cfg.port; 58 HOST = cfg.host; 59 ERL_EPMD_ADDRESS = "127.0.0.1"; 60 BASE_URL = cfg.baseUrl; 61 DB_PATH = "${cfg.dataDir}/shortener.db"; 62 RELEASE_COOKIE = "shirts-cookie"; 63 RELEASE_NODE = "shirts@localhost"; 64 RELEASE_TMP = "${cfg.dataDir}/tmp"; 65 ERL_AFLAGS = "-kernel inet_dist_use_interface '{127,0,0,1}'"; 66 }; 67 68 serviceConfig = { 69 Type = "simple"; 70 LoadCredential = "auth_token:${toString cfg.authTokenFile}"; 71 ExecStart = pkgs.writeShellScript "shirts-start" '' 72 export AUTH_TOKEN=$(cat "$CREDENTIALS_DIRECTORY/auth_token") 73 exec ${shirts}/bin/shortener start 74 ''; 75 ExecStop = "${shirts}/bin/shortener stop"; 76 Restart = "always"; 77 RestartSec = "5s"; 78 User = "shirts"; 79 Group = "shirts"; 80 StateDirectory = "shirts"; 81 WorkingDirectory = cfg.dataDir; 82 RuntimeDirectory = "shirts"; 83 SuccessExitStatus = [0 143]; 84 KillMode = "mixed"; 85 LimitNOFILE = 65535; 86 }; 87 }; 88 89 users = { 90 users.shirts = { 91 isSystemUser = true; 92 group = "shirts"; 93 }; 94 groups.shirts = {}; 95 }; 96 97 services.caddy.virtualHosts = { 98 "${cfg.domain}".extraConfig = '' 99 reverse_proxy 127.0.0.1:${toString cfg.port} 100 redir / https://${haravara.stripSubdomain cfg.domain} 101 ''; 102 }; 103 }; 104 }