Skip to content

Commit

Permalink
Treat modules correctlier
Browse files Browse the repository at this point in the history
  • Loading branch information
Shay Bergmann committed Jun 8, 2021
1 parent 7c5340c commit b50d229
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 82 deletions.
57 changes: 53 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions flake.nix
Expand Up @@ -8,6 +8,7 @@
nixpkgs-unstable.url = "github:nixos/nixpkgs/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
bitte-cli.url = "github:input-output-hk/bitte-cli";
hydra-provisioner.url = "github:input-output-hk/hydra-provisioner";
nix.url = "github:NixOS/nix?rev=b19aec7eeb8353be6c59b2967a511a5072612d99";
ops-lib = {
url = "github:input-output-hk/ops-lib";
Expand All @@ -28,7 +29,7 @@
};
};

outputs = { self, nixpkgs, utils, bitte-cli, ... }@inputs:
outputs = { self, hydra-provisioner, nixpkgs, utils, bitte-cli, ... }@inputs:
let overlay = import ./overlay.nix inputs;
in (utils.lib.eachSystem [ "x86_64-linux" ] (system: rec {

Expand All @@ -38,7 +39,7 @@
overlays = [ overlay ];
};

inherit (legacyPackages) devShell nixosModules;
inherit (legacyPackages) devShell;

packages = {
inherit (legacyPackages)
Expand All @@ -53,7 +54,13 @@
apps.bitte = utils.lib.mkApp { drv = legacyPackages.bitte; };

})) // {
lib = import ./lib { inherit (nixpkgs) lib; };
inherit overlay;
mkHashiStack = import ./lib/mk-hashi-stack.nix;

nixosModules = let
modules = self.lib.mkModules ./modules;
default.imports = [ builtins.attrValues modules ];
in modules // { inherit default; };
};
}
}
7 changes: 7 additions & 0 deletions lib/default.nix
@@ -0,0 +1,7 @@
{ lib }: rec {
recImport = import ./rec-import.nix { inherit lib; };
sanitize = import ./sanitize.nix { inherit lib snakeCase; };
snakeCase = import ./snake-case.nix { inherit lib; };
mkModules = import ./make-modules.nix { inherit lib; };
}

38 changes: 38 additions & 0 deletions lib/make-modules.nix
@@ -0,0 +1,38 @@
{ lib }: dir:

let
join = a: b: if a == "" then b else "${a}-${b}";

inherit (builtins) readDir mapAttrs attrValues foldl' elemAt typeOf substring stringLength listToAttrs;

convert = prefix: d:
let
entries = readDir d;
expanded = mapAttrs (name: type:
if type == "regular" then [
(join prefix name)
(d + "/${name}")
] else if type == "directory" then [
(join prefix name)
(convert (join prefix name) (d + "/${name}"))
] else
null) entries;
in attrValues expanded;

tree = convert "" dir;

result = sum: input:
foldl' (s: elems:
let
cat = elemAt elems 0;
car = elemAt elems 1;
in if typeOf car == "list" then
(result s car)
else
s ++ [{
name = substring 0 ((stringLength cat) - 4) cat;
value = import car;
}]) sum input;

folded = result [ ] tree;
in listToAttrs folded
19 changes: 11 additions & 8 deletions lib/rec-import.nix
@@ -1,21 +1,24 @@
{ lib }:
let
inherit (builtins) attrNames readDir;
inherit (lib) filterAttrs hasSuffix mapAttrs' nameValuePair;
inherit (lib) filterAttrs hasSuffix removeSuffix mapAttrs' nameValuePair;

# mapFilterAttrs ::
# (name -> value -> bool )
# (name -> value -> { name = any; value = any; })
# attrs
mapFilterAttrs = sieve: f: attrs: filterAttrs sieve (mapAttrs' f attrs);

in { dir, _import ? base: import "${dir}/${base}.nix" }:
mapFilterAttrs (_: v: v != null) (n: v:
recImport = { dir, _import ? base: builtins.trace "importing ${toString dir} ${base}" (import (dir + "/${base}.nix")) }:
mapFilterAttrs (_: v: v != null) (n: v:
if n != "default.nix"
&& ((hasSuffix ".nix" n && v == "regular" && false) || v == "directory")
&& ((hasSuffix ".nix" n && v == "regular") || v == "directory")

then
let name = n; in nameValuePair (name) (_import name)
then let
baseName = removeSuffix ".nix" n;
in nameValuePair (baseName) (if v == "regular" then _import baseName else recImport { dir = dir + "/${baseName}"; })

else nameValuePair ("") (null)) (readDir dir);

in recImport

else
nameValuePair ("") (null)) (readDir dir)
3 changes: 1 addition & 2 deletions lib/sanitize.nix
@@ -1,7 +1,6 @@
{ pkgs }:
{ lib, snakeCase }:
let
inherit (builtins) typeOf;
inherit (pkgs) lib snakeCase;
inherit (lib) length attrNames pipe filterAttrs nameValuePair mapAttrs';

sanitize = obj:
Expand Down
2 changes: 1 addition & 1 deletion lib/snake-case.nix
@@ -1,4 +1,4 @@
{ lib, ... }:
{ lib }:
lib.flip lib.pipe [
(builtins.split "([^a-z])")
(lib.concatMapStrings (s: if builtins.isList s then "_${toString s}" else s))
Expand Down
9 changes: 9 additions & 0 deletions modules/disabled-modules.nix
@@ -0,0 +1,9 @@
{ ... }: {
# NOTE Shouldn't these just go inside their respective modules?
disabledModules = [
"services/databases/victoriametrics.nix"
"services/monitoring/telegraf.nix"
"services/networking/consul.nix"
"services/security/vault.nix"
];
}
Expand Up @@ -36,7 +36,7 @@ let
};

owner = mkOption {
type = enum (lib.attrNames config.services.hydra.users);
type = enum (attrNames config.services.hydra.users);
};

declfile = mkOption {
Expand Down Expand Up @@ -114,4 +114,51 @@ in {
default = {};
};
};

config = mkIf cfg.enable {
systemd.services.hydra-declarative = {
description = "Hydra declarative projects and users";
wantedBy = [ "multi-user.target" ];
requires = [ "hydra-init.service" "postgresql.service" ];
after = [ "hydra-init.service" "postgresql.service" ];

path = [ config.services.postgresql.package ];

serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
User = "hydra";
};

script = ''
cat <<EOF | psql
BEGIN;
DELETE FROM users WHERE username not in (${concatMapStringsSep "," (username: "'${username}'") (attrNames cfg.users)});
DELETE FROM userroles;
UPDATE projects SET enabled = 0;
${concatMapStringsSep "\n" (username: with cfg.users.${username}; let
cols = "(username,fullname,emailaddress,password,emailonerror,type,publicdashboard)";
vals = "('${email}','${fullName}','${email}','!',${emailOnError},'google','${publicDashboard}')";
in ''
INSERT INTO users ${cols} VALUES ${vals} ON CONFLICT (username) DO UPDATE SET ${cols} = ${vals};
${concatMapStringsSep "\n" (role: ''
INSERT INTO userroles (username,role) VALUES ('${username}','${role}');
'') roles}
'') (attrNames cfg.users)}
${concatMapStringsSep "\n" (projectName: with cfg.projects.${projectName}; let
cols = "(name,declfile,decltype,declvalue,displayname,description,homepage,owner,enabled,hidden)";
vals = "('${projectName}','${declfile}','${decltype}','${declvalue}','${displayName}','${description}','${homepage}','${owner}',${enable},${hidden})";
in ''
INSERT INTO projects ${cols} VALUES ${vals} ON CONFLICT (name) DO UPDATE SET ${cols} = ${vals};
'') (attrNames cfg.projects)}
COMMIT;
EOF
'';
};
};
}
3 changes: 3 additions & 0 deletions modules/hydra-provisioner.nix
@@ -0,0 +1,3 @@
{ self, ... }: {
imports = [ self.inputs.hydra-provisioner.nixosModules.hydra-provisioner ];
}
53 changes: 0 additions & 53 deletions modules/hydra/declarative/config.nix

This file was deleted.

3 changes: 0 additions & 3 deletions modules/hydra/declarative/default.nix

This file was deleted.

7 changes: 0 additions & 7 deletions modules/hydra/default.nix

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit b50d229

Please sign in to comment.