From a6707d1448a1c9fda51f5d6f33716bd5b6dd71d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Sun, 29 Sep 2024 18:31:09 +0200 Subject: [PATCH] nix: +nixosModules.typst-bot --- nix/nixosModules/default.nix | 2 + nix/nixosModules/typst-bot/default.nix | 79 ++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 nix/nixosModules/typst-bot/default.nix diff --git a/nix/nixosModules/default.nix b/nix/nixosModules/default.nix index aa53ca7b..10762033 100644 --- a/nix/nixosModules/default.nix +++ b/nix/nixosModules/default.nix @@ -18,6 +18,8 @@ let "zram" "i_am_builder" "fish-vendor-completions" + + "typst-bot" ]; sharedModules.imports = map toModule sharedModuleNames; diff --git a/nix/nixosModules/typst-bot/default.nix b/nix/nixosModules/typst-bot/default.nix new file mode 100644 index 00000000..c2b2368d --- /dev/null +++ b/nix/nixosModules/typst-bot/default.nix @@ -0,0 +1,79 @@ +{ + pkgs, + config, + lib, + ... +}: + +let + cfg = config.services.typst-bot; + t = lib.types; +in + +{ + + options = { + + services.typst-bot = { + + enable = lib.mkEnableOption "typst-bot"; + + dataDir = lib.mkOption { + description = "Cache directory"; + type = t.path; + default = "/var/typst-bot"; + }; + + environmentFile = lib.mkOption { + description = "Path to an environment file, you can set the token there"; + type = t.path; + }; + + }; + + }; + + config = lib.mkIf cfg.enable { + + users.users."typst-bot" = { + group = "typst-bot"; + isSystemUser = true; + }; + users.groups."typst-bot" = { }; + + systemd.tmpfiles.rules = [ + "d ${cfg.dataDir}/cache 700 typst-bot typst-bot - -" + "d ${cfg.dataDir}/sqlite 700 typst-bot typst-bot - -" + ]; + + systemd.services."typst-bot" = { + description = "A discord bot to render Typst code"; + after = [ "network.target" ]; + wantedBy = [ "multi-user.target" ]; + + preStart = '' + touch ${cfg.dataDir}/sqlite/db.sqlite + ''; + + # Don't pollute the global path + path = [ pkgs.myPkgs.typst-bot ]; + script = "typst-bot"; + + serviceConfig = { + User = "typst-bot"; + Group = "typst-bot"; + + EnvironmentFile = [ + "${pkgs.writeText "typst-bot-env" '' + DB_PATH=${cfg.dataDir}/sqlite/db.sqlite + CACHE_DIRECTORY=${cfg.dataDir}/cache + ''}" + cfg.environmentFile + ]; + }; + + }; + + }; + +}