From f684ad7f5590f418f5d01288ca229474402afc88 Mon Sep 17 00:00:00 2001 From: Stephan Maka Date: Tue, 30 Apr 2019 22:38:58 +0200 Subject: [PATCH] homu: prepare nixos integration gitea issue #10 --- nixbld-etc-nixos/configuration.nix | 12 +++ nixbld-etc-nixos/homu/nixos-module.nix | 92 +++++++++++++++++++++ nixbld-etc-nixos/{homu.nix => homu/pkg.nix} | 2 +- 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 nixbld-etc-nixos/homu/nixos-module.nix rename nixbld-etc-nixos/{homu.nix => homu/pkg.nix} (88%) diff --git a/nixbld-etc-nixos/configuration.nix b/nixbld-etc-nixos/configuration.nix index 4dce907..ae610f1 100644 --- a/nixbld-etc-nixos/configuration.nix +++ b/nixbld-etc-nixos/configuration.nix @@ -8,6 +8,7 @@ imports = [ # Include the results of the hardware scan. ./hardware-configuration.nix + ./homu/nixos-module.nix ]; # Use the systemd-boot EFI boot loader. @@ -243,6 +244,17 @@ ACTION=="add", SUBSYSTEM=="tty", \ }; }; + # services.homu = { + # enable = true; + # # See https://github.com/servo/homu/blob/master/cfg.sample.toml + # config = { + # max_priority = 9001; + # github = { + # access_token = "..."; + # }; + # }; + # }; + # This value determines the NixOS release with which your system is to be # compatible, in order to avoid breaking some software such as database # servers. You should change this only after NixOS release notes say you diff --git a/nixbld-etc-nixos/homu/nixos-module.nix b/nixbld-etc-nixos/homu/nixos-module.nix new file mode 100644 index 0000000..22f2d70 --- /dev/null +++ b/nixbld-etc-nixos/homu/nixos-module.nix @@ -0,0 +1,92 @@ +{ config, pkgs, lib, ... }: +with lib; +let + homu = pkgs.callPackage ./pkg.nix {}; + + toToml = key: value: + let valueString = + if builtins.isString value + then "\"" + (builtins.replaceStrings ["\"" "\\"] ["\\\"" "\\\\"] value) + "\"" + else toString value; + in "${key} = ${valueString}\n"; + + defaultConfig = { + db = { + file = "/var/db/homu/main.db"; + }; + }; + cfg = config.services.homu; + homuConfig = defaultConfig // cfg.config; + configFilter = f: + filterAttrs (key: value: f value) homuConfig; + topLevelConfig = + configFilter (value: ! builtins.isAttrs value); + configSections = + configFilter (value: builtins.isAttrs value); + + configFile = builtins.toFile "config.toml" ( + builtins.concatStringsSep "" ( + (attrsets.mapAttrsToList toToml topLevelConfig) ++ + (builtins.concatLists (attrsets.mapAttrsToList + (sectionName: sectionConfig: + [ "[${sectionName}]\n" ] ++ + (attrsets.mapAttrsToList toToml sectionConfig) + ) configSections) + )) + ); + + dbFile = homuConfig.db.file; +in + +{ + options.services.homu = { + enable = mkOption { + type = types.bool; + default = false; + description = "Enable the bot"; + }; + user = mkOption { + type = types.str; + default = "nobody"; + }; + group = mkOption { + type = types.str; + default = "nogroup"; + }; + config = mkOption { + description = "Structured data for config.toml"; + type = with types; attrsOf unspecified; + }; + }; + + config = mkIf cfg.enable { + systemd.services.homu-dbdir = { + description = "Homu bot database directory"; + serviceConfig = { + Type = "oneshot"; + ExecStart = [ + "${pkgs.coreutils}/bin/mkdir -p ${dirOf dbFile}" + "${pkgs.coreutils}/bin/chown -R ${cfg.user}:${cfg.group} ${dirOf dbFile}" + ]; + }; + }; + systemd.services.homu = { + description = "Homu bot"; + wantedBy = [ "multi-user.target" ]; + requires = [ "homu-dbdir.service" ]; + after = [ "network.target" ]; + serviceConfig = { + Type = "simple"; + ExecStart = "${homu}/bin/homu -c ${configFile}"; + + Restart = "always"; + RestartSec = "5sec"; + + User = cfg.user; + Group = cfg.group; + }; + }; + }; + +} + diff --git a/nixbld-etc-nixos/homu.nix b/nixbld-etc-nixos/homu/pkg.nix similarity index 88% rename from nixbld-etc-nixos/homu.nix rename to nixbld-etc-nixos/homu/pkg.nix index e2378f9..ee95db2 100644 --- a/nixbld-etc-nixos/homu.nix +++ b/nixbld-etc-nixos/homu/pkg.nix @@ -27,6 +27,6 @@ in rev = "2ea53e76ebac3e5fa11bc39054b3cd4c42eff607"; sha256 = "1ih7s8zfbpq0qb9vqbxzr0r4s9ff52l4ipr916kwbck3ygliq3r9"; }; - buildInputs = [ github3_py_0_9_6 ] ++ (with python3Packages; [ toml jinja2 requests bottle waitress retrying ]); + propagatedBuildInputs = [ github3_py_0_9_6 ] ++ (with python3Packages; [ toml jinja2 requests bottle waitress retrying ]); checkPhase = "python -m unittest discover tests -v"; }