write nixos module

main
Yorick van Pelt 2024-04-27 10:51:51 +02:00
parent 85c81292d8
commit 6eb4056bd3
Signed by: yorick
GPG Key ID: D8D3CC6D951384DE
2 changed files with 54 additions and 18 deletions

View File

@ -5,27 +5,62 @@
inputs.dream2nix.url = "github:nix-community/dream2nix";
outputs = { self, nixpkgs, flake-utils, dream2nix }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
(flake-utils.lib.eachDefaultSystem (system:
let pkgs = nixpkgs.legacyPackages.${system};
in {
packages = {
default = dream2nix.lib.evalModules {
modules = [ {
deps.python = pkgs.python311;
paths = {
projectRoot = ./.;
projectRootFile = "pyproject.toml";
package = ".";
};
} ./package.nix ];
modules = [
{
deps.python = pkgs.python311;
paths = {
projectRoot = ./.;
projectRootFile = "pyproject.toml";
package = ".";
};
}
./package.nix
];
packageSets.nixpkgs = pkgs;
};
};
devShells.default = self.packages.${system}.default.devShell.overrideAttrs (o: {
nativeBuildInputs = o.nativeBuildInputs ++ [ pkgs.ruff pkgs.black pkgs.pyright ];
});
});
devShells.default =
self.packages.${system}.default.devShell.overrideAttrs (o: {
nativeBuildInputs = o.nativeBuildInputs
++ [ pkgs.ruff pkgs.black pkgs.pyright ];
});
})) // {
nixosModules.default = { config, pkgs, lib, ... }:
let cfg = config.services.yobot;
in {
options.services.yobot = with lib; {
enable = mkEnableOption "enable yobot";
package = mkOption {
type = types.package;
default = self.packages.${pkgs.system}.default;
};
configFile = mkOption { type = types.path; };
};
config = lib.mkIf cfg.enable {
systemd.services.yobot = {
wantedBy = [ "multi-user.target" ];
environment.YOBOT_CONFIG = "%d/config.toml";
serviceConfig = {
ExecStart = "${cfg.package}/bin/yobot bot";
DynamicUser = true;
Type = "simple";
LoadCredential = "config.toml:${cfg.configFile}";
RestartSteps = 20;
Restart = "on-failure";
};
unitConfig = {
StartLimitIntervalSec = 300;
StartLimitBurst = 5;
};
};
};
};
};
}

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python
import sys
import os
import yobot.repl
import yobot.bot
from yobot.yobot import Yobot
@ -12,7 +13,7 @@ def main() -> None:
if len(sys.argv) < 2:
print("usage: $0 repl|bot")
sys.exit(1)
bot = Yobot()
bot = Yobot(os.environ.get("YOBOT_CONFIG", "config.toml"))
match sys.argv[1]:
case "repl":
asyncio.run(yobot.repl.main(bot))