all repos — nixfiles @ 5434207aa57186bce175729621e98181a2fce928

System and user configuration, managed by nix and home-manager

dev/web: run caddy as user service with Caddyfile watching

Alin
commit

5434207aa57186bce175729621e98181a2fce928

parent

93288d41342cb9768c35a90d2f7909ebb764ba4a

1 file changed, 86 insertions(+), 0 deletions(-)

changed files
A user/modules/caddy.nix
@@ -0,0 +1,86 @@
+{ + config, + lib, + pkgs, + ... +}: +let + cfg = config.services.caddy; +in +{ + options.services.caddy = { + enable = lib.mkEnableOption "Caddy web server (for local development)"; + + package = lib.mkPackageOption pkgs "caddy" { }; + + configFile = lib.mkOption { + type = lib.types.path; + description = '' + Path to a Caddyfile configuration file. + ''; + }; + + adapter = lib.mkOption { + type = with lib.types; nullOr str; + default = "caddyfile"; + + description = '' + Name of the config adapter to use. + See <https://caddyserver.com/docs/config-adapters> + for the full list. + + If `null` is specified, the `--adapter` argument is omitted when + starting or restarting Caddy. Notably, this allows specification of a + configuration file in Caddy's native JSON format, as long as the + filename does not start with `Caddyfile` (in which case the `caddyfile` + adapter is implicitly enabled). See + <https://caddyserver.com/docs/command-line#caddy-run> for details. + ''; + }; + + watch = lib.mkOption { + type = lib.types.bool; + default = false; + description = '' + Automatically reload the config file, when changed. + ''; + }; + }; + + config = + let + args = [ + "run" + "--config" + "${cfg.configFile}" + ] + ++ lib.optionals (cfg.adapter != null) [ + "--adapter" + cfg.adapter + ] + ++ lib.optional cfg.watch "--watch"; + in + lib.mkIf cfg.enable { + home.packages = [ cfg.package ]; + + launchd.agents.caddy = { + enable = true; + + config = { + ProgramArguments = [ (lib.getExe cfg.package) ] ++ args; + RunAtLoad = true; + }; + }; + + # untested + systemd.user.services.caddy = { + unitConfig = { + wantedBy = [ "default.target" ]; + }; + serviceConfig = { + ExecStart = "${lib.getExe cfg.package} ${lib.escapeShellArgs args}"; + }; + }; + + }; +}