From ffefe1fea1393bef3d1aa83c16e27d998c46ff62 Mon Sep 17 00:00:00 2001 From: clerie Date: Fri, 21 Jul 2023 23:26:00 +0200 Subject: [PATCH] Add NixOS modules --- flake.nix | 16 +++++++++++ nix/module.nix | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 nix/module.nix diff --git a/flake.nix b/flake.nix index 3dab73f..acae174 100644 --- a/flake.nix +++ b/flake.nix @@ -28,6 +28,22 @@ default = self.apps.x86_64-linux.update-from-hydra; }; + nixosModules = { + update-from-hydra = { ... }: { + imports = [ + ./nix/module.nix + ]; + + nixpkgs.overlays = [ + (_: _: { + inherit (self.packages."x86_64-linux") + update-from-hydra; + }) + ]; + }; + default = self.nixosModules.update-from-hydra; + }; + hydraJobs = { inherit (self) packages; diff --git a/nix/module.nix b/nix/module.nix new file mode 100644 index 0000000..2ccc405 --- /dev/null +++ b/nix/module.nix @@ -0,0 +1,78 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.services.update-from-hydra; + + repositoryOpts = { config, ... }@moduleAttrs: { + options = { + + updatePath = mkOption { + type = types.path; + description = '' + Path to the symlink pointing to the hydra build output store path + ''; + }; + + hydraUrl = mkOption { + type = types.str; + example = "https://hydra.clerie.de/job/clerie/chaosevents/packages.x86_64-linux.chaosevents/latest-finished"; + description = '' + Url to Hydra + ''; + }; + + storeUri = mkOption { + type = types.str; + description = '' + Uri to nix store from which the store path build in Hydra can be copied from + ''; + }; + + startAt = mkOption { + type = with types; either str (listOf str); + default = "hourly"; + description = '' + How often the directory should be updated. + Format specified by `systemd.time 7`. + This value can also be a list of `systemd.time 7` formatted + strings, in which case the service will be started on multiple + schedules. + ''; + }; + }; + }; + +in { + options = { + services.update-from-hydra = { + + enable = mkEnableOption "Make a hydra build output availiable with a symlink and keep it up to date"; + + paths = mkOption { + type = with types; attrsOf (submodule repositoryOpts); + default = {}; + }; + + }; + }; + + config = mkIf cfg.enable { + systemd.services = (mapAttrs' (name: path: let + in nameValuePair "update-from-hydra-${name}" { + inherit (path) startAt; + wantedBy = [ "multi-user.target" ]; + serviceConfig = { + Type = "oneshot"; + }; + path = [ pkgs.nix ]; + script = '' + ${pkgs.update-from-hydra}/bin/update-from-hydra "${path.hydraUrl}" "${path.updatePath}" "${path.storeUri}" + ''; + } + ) cfg.paths); + }; +} +