zhaofengli/colmena

Split each node into different files?

szethh opened this issue · 1 comments

Hi, I'm new to nix(os), and I was wondering how to split my colmena config into different files:

This is what works, and what I'm trying to move away from (flake.nix):

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };
  outputs = { nixpkgs, ... }:
  let
    systemLinux = "x86_64-linux";
    systemDarwin = "x86_64-darwin";
    pkgsLinux = import nixpkgs { inherit systemLinux; };
    pkgsDarwin = import nixpkgs { inherit systemDarwin; };
  in
  {
    # nix develop
    devShells.${systemDarwin}.default = pkgsDarwin.mkShell {
      buildInputs = [ pkgsDarwin.colmena ];
    };

    # colmena stuff
    colmena = {
      meta = {
        # for some reason if i do nixpkgs = pkgsLinux; here, it doesn't work
        nixpkgs = import nixpkgs { system = "x86_64-linux"; };
      };

      nixie = { modulesPath, lib, name, ... }:
        {
          imports = lib.optional (builtins.pathExists ./do-userdata.nix) ./do-userdata.nix ++ [
              (modulesPath + "/virtualisation/digital-ocean-config.nix")
              ../common/szeth.nix
          ];

          system.stateVersion = "24.05";

          deployment = { ... };

          networking.hostName = name;
        }
    };
  };
}

I then tried to have a move my host config over to hosts/nixie.nix:

{ modulesPath, lib, name, ... }:

{
  imports = lib.optional (builtins.pathExists ./do-userdata.nix) ./do-userdata.nix ++ [
      (modulesPath + "/virtualisation/digital-ocean-config.nix")
      ../common/szeth.nix
  ];

  system.stateVersion = "24.05";

  deployment = { ... };

  networking.hostName = name;
}

...and then reference it from flake.nix:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };
  outputs = { nixpkgs, ... }@inputs: 
  let
    systemLinux = "x86_64-linux";
    systemDarwin = "x86_64-darwin";
    pkgsLinux = import nixpkgs { inherit systemLinux; };
    pkgsDarwin = import nixpkgs { inherit systemDarwin; };
  in
  {
    # nix develop
    devShells.${systemDarwin}.default = pkgsDarwin.mkShell {
      buildInputs = [ pkgsDarwin.colmena ];
    };

    # colmena stuff
    colmena = {
      meta = {
        # for some reason if i do nixpkgs = pkgsLinux; here, it doesn't work
        nixpkgs = import nixpkgs { system = "x86_64-linux"; };

        specialArgs = {
          inherit inputs;
        };
      };

      # how to pass parameters?????
      nixie = import ./hosts/nixie.nix { name = "nixie"; };# { inherit lib; modulesPath = colmena.meta.nixpkgs.modulesPath; };
    };
  };
}

I added the outputs = { ... }@inputs:, alongside with specialArgs because of this comment, but it still does not work :(

I am super new to nix, and I can't understand how colmena is passing parameters to the nixie function in the first example.
When importing, I need to pass those parameters myself (lib, modulesPath, even name).

How can I do that?
I tried to look for examples but I could not find examples of multi-file configurations anywhere in the docs or GH issues.

I'm sure it's a super simple oversight, but I don't know enough about nix to understand what's going on here :')

solved after learning more about nix syntax. if any other nixos newbie comes across this issue, here's what I did:

# flake.nix
colmena = {
  nixie = import ./hosts/nixie;
}
# hosts/nixie/default.nix
{ modulesPath, lib, name, pkgs, config, ... }:
{
  # digitalocean setup
  imports = lib.optional (builtins.pathExists ./do-userdata.nix) ./do-userdata.nix ++ [
      (modulesPath + "/virtualisation/digital-ocean-config.nix")
  ];

  networking.hostName = name;
  # your config...
}