aboutsummaryrefslogtreecommitdiff
path: root/modules/deployment
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2016-09-29 13:51:44 +0300
committerIgor Pashev <pashev.igor@gmail.com>2016-09-29 13:51:44 +0300
commit62f28d30a069135f9c48678507203958adfc334f (patch)
tree7f38af0c8d3f445ee8cc50906a639baec7011127 /modules/deployment
parent1af9e6589bdd18e6ba7eeabf073aa7d710020cdd (diff)
downloadnixsap-62f28d30a069135f9c48678507203958adfc334f.tar.gz
Moved everything into ./modules
Diffstat (limited to 'modules/deployment')
-rw-r--r--modules/deployment/default.nix11
-rw-r--r--modules/deployment/keyrings.nix64
2 files changed, 75 insertions, 0 deletions
diff --git a/modules/deployment/default.nix b/modules/deployment/default.nix
new file mode 100644
index 0000000..240d970
--- /dev/null
+++ b/modules/deployment/default.nix
@@ -0,0 +1,11 @@
+{lib, ... }:
+
+let
+ all = lib.filterAttrs
+ ( n: _: n != "default.nix" && ! lib.hasPrefix "." n )
+ (builtins.readDir ./.);
+
+in {
+ imports = map (p: ./. + "/${p}") ( builtins.attrNames all );
+}
+
diff --git a/modules/deployment/keyrings.nix b/modules/deployment/keyrings.nix
new file mode 100644
index 0000000..6230107
--- /dev/null
+++ b/modules/deployment/keyrings.nix
@@ -0,0 +1,64 @@
+{ config, lib, ... }:
+
+let
+
+ inherit (builtins)
+ attrNames baseNameOf head match pathExists readFile toString ;
+ inherit (lib)
+ foldl genAttrs mapAttrsToList mkOption optionalAttrs types ;
+ inherit (types)
+ attrsOf listOf nullOr path ;
+
+ allusers = config.users.users;
+ cfg = config.nixsap.deployment;
+
+ # XXX If the file is encrypted:
+ # error: the contents of the file ‘...’ cannot be represented as a Nix string
+ read = key:
+ let
+ m = match "^([^(]*)\\[.+\\]$" key;
+ s = if m != null then head m else key;
+ in if cfg.secrets != null
+ then readFile (cfg.secrets + "/${s}")
+ else "";
+
+in {
+ options.nixsap.deployment = {
+ secrets = mkOption {
+ description = ''
+ Directory with the secrets. If not specified,
+ each key will be an empty file.
+ '';
+ type = nullOr path;
+ default = null;
+ example = "<secrets>";
+ };
+ keyrings = mkOption {
+ type = attrsOf (listOf path);
+ description = ''
+ Binds keys to a user. It's possible to share the same key between
+ multiple users, of course by different names: "/run/keys/foo" and
+ "/run/keys/foo[bar]" will use the same secret file "foo".
+ '';
+ default = {};
+ example = { mysqlbackup = [ "/run/keys/s3cmd.cfg" ];
+ pgbackup = [ "/run/keys/s3cmd.cfg[pgbackup]" ];
+ };
+ };
+ };
+
+ config = {
+ users.users = genAttrs (attrNames cfg.keyrings) (
+ name: optionalAttrs (name != "root") { extraGroups = [ "keys" ]; }
+ );
+
+ deployment.keys = foldl (a: b: a//b) {} (
+ mapAttrsToList (name: keys:
+ genAttrs (map baseNameOf keys)
+ (key: { text = read key;
+ user = toString allusers.${name}.uid;
+ })
+ ) cfg.keyrings
+ );
+ };
+}