Skip to content

Instantly share code, notes, and snippets.

@shlevy
Created February 19, 2014 16:54
Show Gist options
  • Save shlevy/9096240 to your computer and use it in GitHub Desktop.
Save shlevy/9096240 to your computer and use it in GitHub Desktop.
{ lib, config, options, ... }:
with lib;
let
baseArgs = {
inherit nodes;
modules = [];
modulesPath = <nixpkgs/nixos/modules>;
inherit (config) resources;
};
# Evaluate just the misc/nixpkgs module and passed-in modules
# so we can get nixpkgs.config and nixpkgs.system
stage1Eval = name:
let
defs = map (getAttr name) (filter (hasAttr name) options.resources.machines.definitions);
coerce = def: if builtins.isFunction def then def else { config = def; };
baseModules = [ <nixpkgs/nixos/modules/misc/nixpkgs.nix> ];
modules = baseModules
++ map (def: { _file = def.file; imports = [(coerce def.value)]; }) defs;
args = baseArgs // rec {
inherit name baseModules;
pkgs = import <nixpkgs> {};
pkgs_i686 = pkgs.pkgsi686Linux;
utils = import <nixpkgs/nixos/lib/utils.nix> pkgs;
};
in (evalModules { inherit modules args; check = false; }).config.nixpkgs;
nodes = config.resources.machines;
baseModules = [ ./options.nix ] ++
import <nixpkgs/nixos/modules/module-list.nix>;
submoduleFn = name: types.submoduleWithExtraArgs (baseModules ++ [ ({ name, lib, ... }: {
_file = ./machine.nix;
key = ./machine.nix;
config = {
networking.hostName = lib.mkOverride 900 name;
deployment.targetHost = lib.mkOverride 900 name;
};
}) ]) (baseArgs // rec {
inherit baseModules;
pkgs = import <nixpkgs> {
inherit (stage1Eval name) system config;
};
pkgs_i686 = pkgs.pkgsi686Linux;
utils = import <nixpkgs/nixos/lib/utils.nix> pkgs;
});
in {
options = {
resources.machines = mkOption {
default = {};
description = "The machines in the network.";
type = types.dependentAttrsOf submoduleFn;
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment