From e89cf20d4310070a877c2e24a287659546b561c9 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 28 Feb 2018 21:02:21 +0100 Subject: import stockholm's deployment tools https://cgit.krebsco.de/stockholm 877b4104370c1ea9698a449e376e2842d7c372fd --- lib/default.nix | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 lib/default.nix (limited to 'lib/default.nix') diff --git a/lib/default.nix b/lib/default.nix new file mode 100644 index 0000000..7197fe9 --- /dev/null +++ b/lib/default.nix @@ -0,0 +1,56 @@ +let { + + body = lib; + + lib = nixpkgs.lib // builtins // { + + evalSource = let + eval = source: lib.evalModules { + modules = lib.singleton { + _file = toString ./.; + imports = map (source: { inherit source; }) (lib.toList source); + options.source = lib.mkOption { + default = {}; + type = lib.types.attrsOf lib.types.source; + }; + }; + }; + sanitize = x: lib.getAttr (lib.typeOf x) { + set = lib.mapAttrs + (lib.const sanitize) + (lib.filterAttrs + (name: value: name != "_module" && value != null) x); + string = x; + }; + in + # This function's return value can be used as pkgs.populate input. + source: sanitize (eval source).config.source; + + getHostName = let + # We're parsing /etc/hostname here because reading + # /proc/sys/kernel/hostname yields "" + y = lib.filter lib.types.label.check (lib.splitString "\n" (lib.readFile /etc/hostname)); + in + if lib.length y != 1 then throw "malformed /etc/hostname" else + lib.elemAt y 0; + + mkTarget = s: let + default = defVal: val: if val != null then val else defVal; + parse = lib.match "(([^@]+)@)?(([^:/]+))?(:([^/]+))?(/.*)?" s; + elemAt' = xs: i: if lib.length xs > i then lib.elemAt xs i else null; + in { + user = default (lib.getEnv "LOGNAME") (elemAt' parse 1); + host = default (lib.maybeEnv "HOSTNAME" lib.getHostName) (elemAt' parse 3); + port = default "22" /* "ssh"? */ (elemAt' parse 5); + path = default "/var/src" /* no default? */ (elemAt' parse 6); + }; + + test = re: x: lib.isString x && lib.testString re x; + testString = re: x: lib.match re x != null; + + types = nixpkgs.lib.types // import ./types { lib = body; }; + }; + + nixpkgs.lib = import ; + +} -- cgit v1.2.3