xtruder / kubenix

Replaced by https://github.com/hall/kubenix
MIT License
300 stars 34 forks source link

using 'kubernetes.imports' in a module results in an error #20

Closed euank closed 4 years ago

euank commented 4 years ago

The issue I'm seeing seems only happens when using a module in a separate file, so it might be me misunderstanding modules. Or maybe it's a bug in kubenix because I'm not doing anything unusual I think.

Here's a repro for the error I'm seeing:

# ==> repro.nix <==
{
  nixpkgs ? import <nixpkgs> {},
  kubenix ? import (nixpkgs.fetchgit {
    url = "https://github.com/xtruder/kubenix/";
    rev = "611059a329493a77ec0e862fcce4671cd3768f32";
    sha256 = "1lmmzb087ahmx2mdjarbi52a9424qczhzqbxrvcrg11cbmv9b191";
  }) {}
}:
rec {
  config = (kubenix.evalModules {
    modules = [
      ./module.nix { inherit kubenix; }
    ];
  }).config;

  generated = config.kubernetes.generated;
  result = config.kubernetes.result;
}

# ==> module.nix <==
{ config, lib, pkgs, kubenix, ... }:

with kubenix.lib;

let exampleYaml = pkgs.writeText "namespace.yaml" ''
apiVersion: v1
kind: Namespace
metadata:
  name: default
'';
in
{
  imports = with kubenix.modules; [
    k8s
  ];

  kubernetes.imports = [
    exampleYaml
  ];
}

If I try to build that kubernetes object, I get:

$ nix-build repro.nix
error: The option `kubenix.buildResources' defined in `<unknown-file>' does not exist.
(use '--show-trace' to show detailed location information)

I think it's related to modules because if I shove it all in one file, like so, then it builds correctly:

```nix { nixpkgs ? import {}, kubenix ? import (nixpkgs.fetchgit { url = "https://github.com/xtruder/kubenix/"; rev = "611059a329493a77ec0e862fcce4671cd3768f32"; sha256 = "1lmmzb087ahmx2mdjarbi52a9424qczhzqbxrvcrg11cbmv9b191"; }) {} }: let exampleYaml = nixpkgs.writeText "namespace.yaml" '' apiVersion: v1 kind: Namespace metadata: name: default ''; in rec { config = (kubenix.evalModules { modules = [ { imports = with kubenix.modules; [ k8s ]; kubernetes.imports = [ exampleYaml ]; } ]; }).config; generated = config.kubernetes.generated; result = config.kubernetes.result; } ```

One other thing to note: after poking through the stacktrace, I realized the inscrutable error may have actually been trying to say "kubenix.project wasn't set".

If I delete the two lines in modules/k8s.nix that reference config.kubenix.project (https://github.com/xtruder/kubenix/blob/611059a329493a77ec0e862fcce4671cd3768f32/modules/k8s.nix#L361 and line 388), my repro no longer errors out and produces something that works for me.

My totally uneducated understanding is that kubenix.evalModules is breaking the default for project = "kubenix" somehow, and when I don't split the module into a separate file, I don't see this issue because I guess I'm using the kubenix from the outer scope anyway? Yeah, I'm not totally sure.

euank commented 4 years ago

After sleeping on it, I've realized what I'm doing wrong.

    modules = [
      ./module.nix { inherit kubenix; }
    ];

should have been

    modules = [
      ./module.nix { }
    ];

I think evalModules is already plumbing in a correctly setup kubenix, and inherit was overriding that with one from the outer scope.

Now that I check the example in this repo, I also see that it's doing the right thing, so I'm not sure why I had that there.

Sorry for the noise, I'll close this since I figured it out

offlinehacker commented 4 years ago

No problem, sorry for late response, have not been working on kubenix for some time.