Skip to content
Snippets Groups Projects
machine-manager.nix 4.13 KiB
Newer Older
# entry point for machine configurations:
# (import <repo-path> { machinesDir=./machines }).<netname>.configurations.<hostname>
{ flakeInputs, flakeOutputs, machinesDir, extraLayersDir, extraOverlays ? [] }:
Jens Nolte's avatar
Jens Nolte committed
with flakeInputs.nixpkgs.lib;
Jens Nolte's avatar
Jens Nolte committed
  # defaultChannel :: path (channel)
  #defaultChannel = loadChannel "nixos-unstable";

  # helpers :: { *: ? }
  helpers = import ./helpers.nix;

  # channelsDir :: path
  #channelsDir = ./channels;
Jens Nolte's avatar
Jens Nolte committed
  # loadChannel :: string -> path (channel)
  #loadChannel = name: import (channelsDir + "/${name}") name;
Jens Nolte's avatar
Jens Nolte committed
  # allChannels :: { *: path (channel) }
  #allChannels = with helpers; keysToAttrs loadChannel (readFilterDir (filterAnd [(not filterDirHidden) filterDirDirs]) channelsDir);
  # getMachineChannel :: string -> path
  getMachineChannel = _: flakeInputs.nixpkgs;
  #getMachineChannel = { name, path }:
  #  let
  #    channelFile = path + "/channel.nix";
  #  in
  #    if (pathExists channelFile)
  #      then (import channelFile) allChannels
  #      else defaultChannel;
  # machineChannels :: { *: path }
  machineChannels = withMachines getMachineChannel;

  machinesDirContents = readDir machinesDir;
  machineNames = filter (p: machinesDirContents.${p} == "directory") (attrNames machinesDirContents);
  withMachines = lambda: listToAttrs (map (m: {name = m; value = lambda { name = m; path = (machinesDir + "/${m}"); }; }) machineNames);
  evaluateConfig = pkgs: args: (import "${pkgs}/nixos/lib/eval-config.nix" args).config;
Jens Nolte's avatar
Jens Nolte committed
  mkNixosSystemDerivations = { name, path }:
      channel = flakeInputs.nixpkgs;
      system = "x86_64-linux";
      mkMachineConfig = { name, path, isIso }: {
        imports = [
          (import ./configuration.nix {
            inherit name path isIso extraLayersDir flakeInputs flakeOutputs system extraOverlays;
            channel = machineChannels.${name};
          })
        ];
        _module.args.flakeInputs = flakeInputs;
        _module.args.flakeOutputs = flakeOutputs;
        _module.args.system = system;
      };
      configuration = mkMachineConfig { inherit name path; isIso = false; };
      isoConfiguration = mkMachineConfig { inherit name path; isIso = true; };
      iso = (evaluateConfig channel {
        inherit system;
        modules = [
          isoConfiguration
          (mkAdditionalIsoConfig name)
      }).system.build.isoImage;
      sdImage = (evaluateConfig channel {
        inherit system;
        modules = [
          isoConfiguration
          (mkAdditionalSdCardConfig name)
        ];
      }).system.build.sdImage;
Jens Nolte's avatar
Jens Nolte committed
      systemDerivation = channel.lib.nixosSystem {
        inherit system;
        modules = [ configuration ];
      };
    in {
      inherit systemDerivation iso sdImage;
    };
  mkAdditionalIsoConfig = name: { config, modulesPath, ... }: {
    imports = [
      "${modulesPath}/installer/cd-dvd/iso-image.nix"
      "${modulesPath}/profiles/all-hardware.nix"
      "${modulesPath}/profiles/base.nix"
    ];
    isoImage.isoName = "${config.isoImage.isoBaseName}-${config.system.nixos.label}-isohost-${name}.iso";
    isoImage.volumeID = substring 0 11 "NIXOS_ISO";
    isoImage.makeEfiBootable = true;
    isoImage.makeUsbBootable = true;
    boot.loader.grub.memtest86.enable = true;
    _module.args.isIso = true;
  };
  mkAdditionalSdCardConfig = name: { config, modulesPath, ... }: {
    imports = [
      "${modulesPath}/installer/cd-dvd/sd-image.nix"
      "${modulesPath}/profiles/all-hardware.nix"
      "${modulesPath}/profiles/base.nix"
    ];
    sdImage.populateRootCommands = "";
    sdImage.populateFirmwareCommands = "";
    boot.loader.grub.enable = false;
    boot.loader.generic-extlinux-compatible.enable = true;
    _module.args.isIso = true;
  };
  # TODO remove
  # configurations = withMachines mkMachineConfig;
  # nixosIsoDerivations = withMachines mkNixosIsoDerivation;
  # channels = machineChannels;

Jens Nolte's avatar
Jens Nolte committed
  nixosSystemDerivations = withMachines (x: (mkNixosSystemDerivations x).systemDerivation);
  isos = withMachines (x: (mkNixosSystemDerivations x).iso);
  sdImages = withMachines (x: (mkNixosSystemDerivations x).sdImage);
  machineTemplates = withMachines ({name, path}: import (path + /template.nix));