From edcd0a4ddf59dc9cbacf539e019da0c65896be5e Mon Sep 17 00:00:00 2001 From: uku Date: Wed, 29 Jan 2025 11:34:29 +0100 Subject: [PATCH] nixos/shlink: init --- flake.nix | 1 + modules/shlink.nix | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 modules/shlink.nix diff --git a/flake.nix b/flake.nix index ef67f6b..e98c912 100644 --- a/flake.nix +++ b/flake.nix @@ -39,6 +39,7 @@ nixosModules = { reposilite = import ./modules/reposilite.nix; asus-numpad = import ./modules/asus-numpad.nix self; + shlink = import ./modules/shlink.nix self; }; formatter = forEachSystem (system: (pkgsFor system).nixfmt-rfc-style); diff --git a/modules/shlink.nix b/modules/shlink.nix new file mode 100644 index 0000000..2945329 --- /dev/null +++ b/modules/shlink.nix @@ -0,0 +1,87 @@ +self: +{ + lib, + config, + pkgs, + ... +}: +let + cfg = config.services.shlink; + inherit (pkgs.stdenv.hostPlatform) system; + + basePath = "${cfg.package}/share/php/shlink"; + + vars = builtins.map (a: "--set ${a.name} ${a.value}") (lib.attrsToList cfg.environment); + varsStr = lib.concatStringsSep " " vars; + wrappedPkg = pkgs.symlinkJoin { + name = "shlink-wrapped"; + paths = [ cfg.package ]; + nativeBuildInputs = [ pkgs.makeWrapper ]; + + postBuild = '' + wrapProgram $out/bin/shlink ${varsStr} + ''; + }; +in +{ + options.services.shlink = { + enable = lib.mkEnableOption "shlink"; + package = lib.mkPackageOption self.packages.${system} "shlink" { }; + roadrunnerPackage = lib.mkPackageOption pkgs "roadrunner" { }; + + environment = lib.mkOption { + type = lib.types.attrsOf lib.types.str; + default = { }; + description = "Environment variables passed to the RoadRunner process"; + }; + }; + + config = lib.mkIf cfg.enable { + assertions = [ + { + assertion = (cfg.environment.DB_DRIVER ? "sqlite") != "sqlite"; + message = "sqlite is not supported. please set DB_DRIVER to one of the other supported values"; + } + ]; + + environment.systemPackages = [ wrappedPkg ]; + + users = { + groups.shlink = { }; + users.shlink = { + isSystemUser = true; + group = "shlink"; + }; + }; + + systemd.services."shlink" = { + wantedBy = [ "default.target" ]; + path = [ cfg.package.php ]; + environment = cfg.environment // { + SHLINK_RUNTIME = "nixos"; # writes logs to stderr instead of a file inside the nix store + }; + + preStart = '' + export SHELL_VERBOSITY=3 + cd ${basePath} + php vendor/bin/shlink-installer init --no-interaction --clear-db-cache --skip-download-geolite + ''; + + serviceConfig = { + Type = "notify"; + + ExecStart = "${lib.getExe cfg.roadrunnerPackage} serve -c ${basePath}/config/roadrunner/.rr.yml"; + + User = "shlink"; + Group = "shlink"; + + StateDirectory = "shlink"; + + KillMode = "mixed"; + TimeoutStopSec = 30; + Restart = "always"; + RestartSec = 30; + }; + }; + }; +}