diff --git a/config.nix b/config.nix index da6b915..5e851ef 100644 --- a/config.nix +++ b/config.nix @@ -50,7 +50,9 @@ in rec { resource = (inNamespace "hcloud" { - ssh_key = setNames (lib.mapAttrs (_: v: { public_key = v; }) my-lib.ssh-keys); + ssh_key = setNames + (lib.mapAttrs (_: v: { public_key = v; }) + (my-lib.dirContents ".pub" ./ssh-keys)); }); diff --git a/jobs/.gitkeep b/jobs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/lib/default.nix b/lib/default.nix index 68c03b1..1d8b71e 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,5 +1,36 @@ { lib, ... }: +let + + # ".ext" -> ./subdir -> { "foo" = ""; "bar" = ""; } + dirContents = let + + # ".ext" -> "a/b.ext" -> "b" + fileAttrName = suffix: path: let + ext = lib.last (lib.splitString "." path); + in lib.removeSuffix suffix (builtins.baseNameOf path); + + # maps a file to a path + # ".ext" -> "a/b" -> "c/d.ext" -> { name = "d"; value = "a/b/c/d.ext"; } + fileAttrInPath = suffix: path: name: { + name = fileAttrName suffix name; + value = path + "/${name}"; + }; + + # get an object of files in a directory with a given suffix + # ".ext" -> "a/b" -> { "foo" = "a/b/foo.ext"; "bar" = "a/b/bar.ext"; } + dirAttrs = suffix: path: lib.mapAttrs' + (name: _: fileAttrInPath suffix path name) + (lib.filterAttrs + (name: type: lib.hasSuffix suffix name && type == "regular") + (builtins.readDir path)); + + in suffix: path: lib.mapAttrs (_: lib.readFile) (dirAttrs suffix path); + +in + { - ssh-keys = import ./ssh-keys.nix { inherit lib; }; + + inherit dirContents; + } diff --git a/lib/ssh-keys.nix b/lib/ssh-keys.nix deleted file mode 100644 index c6402e7..0000000 --- a/lib/ssh-keys.nix +++ /dev/null @@ -1,25 +0,0 @@ -{ lib, ... }: - -let - - # "a/b.ext" -> "b" - fileAttrName = path: let - ext = lib.last (lib.splitString "." path); - in lib.removeSuffix ".${ext}" (builtins.baseNameOf path); - - # maps a file to a path - # "a/b" -> "c/d.ext" -> { name = "d"; value = "a/b/c/d.ext"; } - fileAttrInPath = path: name: { - name = fileAttrName name; - value = path + "/${name}"; - }; - - # get an object of files in a directory with a given suffix - # "a/b" -> { "foo" = "a/b/foo.ext"; "bar" = "a/b/bar.ext"; } - dirAttrs = suffix: path: lib.mapAttrs' - (name: _: fileAttrInPath path name) - (lib.filterAttrs - (name: type: lib.hasSuffix suffix name && type == "regular") - (builtins.readDir path)); - - in lib.mapAttrs (_: lib.readFile) (dirAttrs ".pub" ../ssh-keys) diff --git a/nomad.nix b/nomad.nix index 8dc2c16..8d241ff 100644 --- a/nomad.nix +++ b/nomad.nix @@ -4,11 +4,15 @@ let var = options.variable; + my-lib = import ./lib/default.nix { inherit lib; }; + in { terraform.required_providers.nomad.source = "registry.terraform.io/hashicorp/nomad"; + # https://developer.hashicorp.com/nomad/docs/job-specification/hcl2/variables + # https://developer.hashicorp.com/nomad/docs/runtime/interpolation variable = { nomad_host = { @@ -40,10 +44,16 @@ in # https://registry.terraform.io/providers/hashicorp/nomad/latest/docs/ resource = { - nomad_job = lib.mapAttrs (k: v: { - json = true; - jobspec = lib.strings.toJSON v; - }) config.nomad.build.apiJob; + nomad_job = + # nix jobs + lib.mapAttrs (_: v: { + json = true; + jobspec = lib.strings.toJSON v; + }) config.nomad.build.apiJob + # hcl jobs + // lib.mapAttrs (_: v: { + jobspec = v; + }) (my-lib.dirContents ".nomad.hcl" ./jobs); };