sheenobu dot net

2021-07-04 - Gn with Nix dependencies

I have been using gn to develop on some small/experimental C codebases. My previous work led me to a different solution, 2021-03-01-nixos-persistent-shell-deps, where I pull dependencies into my local repo via nix-instantiate/nix-store.

Lately, I needed a custom header-only package to be included in my project. I decided to implement it in gn because we get to move our dependency tracking into gn instead of having to use a separate ./configure-style shell script.

Exercises for the reader: 1. use action_foreach instead. I never figured this out. 2. Implement nix_pkg that reads from pkgs instead of from an arbitrary file. 3. Implement some form of versioning.


template("nix_callpkg") {
  action(target_name) {
    script = "//utils/nix.py"
    sources = invoker.sources
    outputs = invoker.outputs
    deps = invoker.deps
    args = rebase_path(sources, root_build_dir) + rebase_path(outputs, root_build_dir)
  }
}

nix_callpkg("stivale2") {
  sources = ["stivale2.nix"]
  deps    = []
  outputs = [ "$target_gen_dir/stivale2" ]
}

executable(...) {
  cflags = [ "-I$target_gen_dir/stivale2/include" ]
  deps = [ ":stivale2" ]
}

def nix(pkg: str, out: str):
    print(f"[NIX] {pkg} -> {out}")
    subprocess.run("nix-build", "-o", out, "-E", f"with import <nixpkgs> {{}}; pkgs.callPackage {pkg} {{}}")

if __name__ == "__main__":
    nix(*sys.argv[1:])
});
← Back to /snippets