self: { config, lib, pkgs, ... }: let inherit (lib) mkEnableOption mkIf mkOption types ; cfg = config.services."vielle.dev"; package = if cfg.spotify.redirectUri == null then cfg.package else cfg.package.override { spotifyRedirectUri = cfg.spotify.redirectUri; }; in { options.services."vielle.dev" = { enable = mkEnableOption "the vielle.dev website (systemd service)"; package = mkOption { type = types.package; default = self.packages.${pkgs.stdenv.hostPlatform.system}.default; defaultText = lib.literalExpression "vielle.dev flake's packages..default"; description = "The vielle.dev package to run."; }; network = { address = mkOption { type = types.str; default = "127.0.0.1"; example = "0.0.0.0"; description = '' Address the server binds to (sets `HOST`). The default only accepts local connections, which suits running behind a reverse proxy. ''; }; port = mkOption { type = types.port; default = 4321; description = "Port the server listens on (sets `PORT`)."; }; }; spotify = { clientId = mkOption { type = types.str; description = "Spotify application client ID (sets `SPOTIFY_CLIENT_ID`)."; }; clientSecretFile = mkOption { type = types.path; example = "/run/secrets/spotify-client-secret"; description = '' Path to a file containing only the Spotify client secret (used for `SPOTIFY_CLIENT_SECRET`). A file is used rather than a plain string so the secret never ends up in the world-readable Nix store. The file is read by systemd as root, so it can be root-only. ''; }; redirectUri = mkOption { type = types.nullOr types.str; default = null; example = "https://vielle.dev/callback"; description = '' OAuth redirect URI registered with Spotify (sets `SPOTIFY_REDIRECT_URI`). When null, the site's own default is used. ''; }; }; }; config = mkIf cfg.enable { users.users."vielle-dev-site" = { isSystemUser = true; group = "vielle-dev-site"; }; users.groups."vielle-dev-site" = { }; systemd.services."vielle.dev" = { description = "vielle.dev website"; wantedBy = [ "multi-user.target" ]; after = [ "network.target" ]; environment = { HOST = cfg.network.address; PORT = toString cfg.network.port; SPOTIFY_CLIENT_ID = cfg.spotify.clientId; } // lib.optionalAttrs (cfg.spotify.redirectUri != null) { SPOTIFY_REDIRECT_URI = cfg.spotify.redirectUri; }; script = '' SPOTIFY_CLIENT_SECRET="$(cat "$CREDENTIALS_DIRECTORY/spotify-client-secret")" export SPOTIFY_CLIENT_SECRET exec ${lib.getExe package} ''; serviceConfig = { User = "vielle-dev-site"; Group = "vielle-dev-site"; StateDirectory = "vielle.dev"; StateDirectoryMode = "0700"; WorkingDirectory = "/var/lib/vielle.dev"; LoadCredential = "spotify-client-secret:${cfg.spotify.clientSecretFile}"; Restart = "on-failure"; NoNewPrivileges = true; PrivateTmp = true; ProtectSystem = "strict"; ProtectHome = true; }; }; }; }