Skip to content

Commit

Permalink
refactor: Get rid of nixpkgs dependency; parse env values in go, use …
Browse files Browse the repository at this point in the history
…nixpkgs.lib
  • Loading branch information
marcusramberg committed May 6, 2024
1 parent f7d12b0 commit bbaa891
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 14 deletions.
18 changes: 7 additions & 11 deletions eval.nix
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
with import <nixpkgs> { };
with builtins;

rec {
yaml2nix =
path:
let
jsonOutputDrv = runCommand "from-yaml" {
nativeBuildInputs = [ yq-go ];
} ''yq -M -o json . "${path}" > "$out"'';
in
fromJSON (readFile jsonOutputDrv);
# pin nixpkgs.lib
lib =
(builtins.getFlake "github:nix-community/nixpkgs.lib/4b620020fd73bdd5104e32c702e65b60b6869426").lib;

# Multiline secret values
mlVals = val: ''
{{ "${val}"|fetchSecretValue }}
'';
# expand secret values
vals = val: ''{{"${val}"|fetchSecretValue}}'';

# render helmfile to object
render =
state: env:
state: env: val:
let
hf = import /${state}/helmfile.nix;
var = {
values = lib.mergeAttrs (yaml2nix /${state}/env/defaults.yaml) (yaml2nix /${state}/env/${env}.yaml);
values = fromJSON (readFile val);
environment.name = env;
};
in
Expand Down
56 changes: 53 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -161,12 +162,25 @@ func JSONToYAMLs(j []byte) ([]byte, error) {
// Render the helmfile using the nix command.
func renderHelmfile(base string, env string) ([]byte, error) {
f, err := writeEvalNix()
defer os.Remove(f.Name())
if err != nil {
log.Fatalf("Could not write eval.nix: %s", err)
}
expr := fmt.Sprintf("(import %s).render \"%s\" \"%s\"", f.Name(), base, env)
cmd := []string{"--extra-experimental-features", "nix-command", "eval", "--json", "--impure", "--expr", expr}
defer os.Remove(f.Name())
val, err := writeValJson(base)
if err != nil {
log.Fatalf("Could not write values.json: %s", err)
}
defer os.Remove(val.Name())
expr := fmt.Sprintf("(import %s).render \"%s\" \"%s\" \"%s\"", f.Name(), base, env, val.Name())
cmd := []string{
"--extra-experimental-features", "nix-command",
"--extra-experimental-features", "flakes",
"eval",
"-I", "nixpkgs=flake:github:nix-community/nixpkgs.lib",
"--json",
"--impure",
"--expr", expr,
}
eval := exec.Command("nix", cmd...)
l.Println("Running nix", strings.Join(cmd, " "))
eval.Stderr = os.Stderr
Expand Down Expand Up @@ -199,3 +213,39 @@ func writeEvalNix() (*os.File, error) {
}
return f, nil
}

func writeValJson(state string) (*os.File, error) {
f, err := os.CreateTemp("", "val.*.json")
if err != nil {
return nil, err
}
defaultVal, err := os.ReadFile(state + "/env/defaults.yaml")
if err != nil {
return nil, err
}
var m map[string]interface{}
err = yaml.Unmarshal(defaultVal, &m)
if err != nil {
return nil, err
}
envVal, err := os.ReadFile(state + "/env/" + opts.Env + ".yaml")
if err != nil {
return nil, err
}
err = yaml.Unmarshal(envVal, &m)
if err != nil {
return nil, err
}
envStr, err := json.Marshal(m)
if err != nil {
return nil, err
}
if _, err := f.Write(envStr); err != nil {
f.Close()
return nil, err
}
if err := f.Close(); err != nil {
return nil, err
}
return f, nil
}
Empty file added testData/helm/env/defaults.yaml
Empty file.
Empty file added testData/helm/env/dev.yaml
Empty file.
Empty file added testData/helm/env/test.yaml
Empty file.

0 comments on commit bbaa891

Please sign in to comment.