mirror of
https://github.com/foo-dogsquared/nixos-config.git
synced 2025-01-31 04:58:01 +00:00
config: modularize image creation functions
Additionally, I have also made a declarative version of the project cluster with a neat little file at `images.toml`. This is way better for individually referring to hosts from the host directory compared to the old way.
This commit is contained in:
parent
5a8cdc5769
commit
f033756d6f
116
flake.nix
116
flake.nix
@ -69,6 +69,14 @@
|
|||||||
|
|
||||||
outputs = inputs@{ self, nixpkgs, ... }:
|
outputs = inputs@{ self, nixpkgs, ... }:
|
||||||
let
|
let
|
||||||
|
# A set of images with their metadata that is usually built for usual
|
||||||
|
# purposes. The format used here is whatever formats nixos-generators
|
||||||
|
# support.
|
||||||
|
images = lib'.importTOML ./images.toml;
|
||||||
|
|
||||||
|
# A set of image-related utilities for the flake outputs.
|
||||||
|
inherit (import ./lib/images.nix { inherit inputs; lib = lib'; }) mkHost mkUser mkImage;
|
||||||
|
|
||||||
# The order here is important(?).
|
# The order here is important(?).
|
||||||
overlays = [
|
overlays = [
|
||||||
# Put my custom packages to be available.
|
# Put my custom packages to be available.
|
||||||
@ -93,15 +101,9 @@
|
|||||||
systems = with inputs.flake-utils.lib.system; [ defaultSystem ];
|
systems = with inputs.flake-utils.lib.system; [ defaultSystem ];
|
||||||
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
|
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
|
||||||
|
|
||||||
# We're considering this as the variant since we'll export the custom
|
|
||||||
# library as `lib` in the output attribute.
|
|
||||||
lib' = nixpkgs.lib.extend (final: prev:
|
|
||||||
import ./lib { lib = prev; }
|
|
||||||
// import ./lib/private.nix { lib = final; });
|
|
||||||
|
|
||||||
extraArgs = {
|
extraArgs = {
|
||||||
inherit (inputs) nix-colors;
|
inherit (inputs) nix-colors;
|
||||||
inherit inputs self;
|
inherit inputs;
|
||||||
|
|
||||||
# This is a variable that is used to check whether the module is
|
# This is a variable that is used to check whether the module is
|
||||||
# exported or not. Useful for configuring parts of the configuration
|
# exported or not. Useful for configuring parts of the configuration
|
||||||
@ -111,19 +113,11 @@
|
|||||||
_isInsideFds = true;
|
_isInsideFds = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
mkHost = { system ? defaultSystem, extraModules ? [ ] }:
|
# We're considering this as the variant since we'll export the custom
|
||||||
(lib'.makeOverridable inputs.nixpkgs.lib.nixosSystem) {
|
# library as `lib` in the output attribute.
|
||||||
# The system of the NixOS system.
|
lib' = nixpkgs.lib.extend (final: prev:
|
||||||
inherit system;
|
import ./lib { lib = prev; }
|
||||||
lib = lib';
|
// import ./lib/private.nix { lib = final; });
|
||||||
specialArgs = extraArgs;
|
|
||||||
modules =
|
|
||||||
# Append with our custom NixOS modules from the modules folder.
|
|
||||||
(lib'.modulesToList (lib'.filesToAttr ./modules/nixos))
|
|
||||||
|
|
||||||
# Our own modules.
|
|
||||||
++ extraModules;
|
|
||||||
};
|
|
||||||
|
|
||||||
# The shared configuration for the entire list of hosts for this cluster.
|
# The shared configuration for the entire list of hosts for this cluster.
|
||||||
# Take note to only set as minimal configuration as possible since we're
|
# Take note to only set as minimal configuration as possible since we're
|
||||||
@ -225,19 +219,6 @@
|
|||||||
services.guix.package = inputs.guix-overlay.packages.${config.nixpkgs.system}.guix;
|
services.guix.package = inputs.guix-overlay.packages.${config.nixpkgs.system}.guix;
|
||||||
};
|
};
|
||||||
|
|
||||||
mkUser = { system ? defaultSystem, extraModules ? [ ] }:
|
|
||||||
inputs.home-manager.lib.homeManagerConfiguration {
|
|
||||||
extraSpecialArgs = extraArgs;
|
|
||||||
lib = lib';
|
|
||||||
pkgs = import nixpkgs { inherit system overlays; };
|
|
||||||
modules =
|
|
||||||
# Importing our custom home-manager modules.
|
|
||||||
(lib'.modulesToList (lib'.filesToAttr ./modules/home-manager))
|
|
||||||
|
|
||||||
# Plus our own.
|
|
||||||
++ extraModules;
|
|
||||||
};
|
|
||||||
|
|
||||||
# The default config for our home-manager configurations. This is also to
|
# The default config for our home-manager configurations. This is also to
|
||||||
# be used for sharing modules among home-manager users from NixOS
|
# be used for sharing modules among home-manager users from NixOS
|
||||||
# configurations with `nixpkgs.useGlobalPkgs` set to `true` so avoid
|
# configurations with `nixpkgs.useGlobalPkgs` set to `true` so avoid
|
||||||
@ -284,29 +265,6 @@
|
|||||||
manpages.enable = true;
|
manpages.enable = true;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
# A wrapper around the nixos-generators `nixosGenerate` function.
|
|
||||||
mkImage = { system ? null, pkgs ? null, extraModules ? [ ], extraArgs ? { }, format ? "iso" }:
|
|
||||||
inputs.nixos-generators.nixosGenerate {
|
|
||||||
inherit pkgs system format;
|
|
||||||
lib = lib';
|
|
||||||
specialArgs = extraArgs;
|
|
||||||
modules =
|
|
||||||
# Import all of the NixOS modules.
|
|
||||||
(lib'.modulesToList (lib'.filesToAttr ./modules/nixos))
|
|
||||||
|
|
||||||
# Our own modules.
|
|
||||||
++ extraModules;
|
|
||||||
};
|
|
||||||
|
|
||||||
# A set of images with their metadata that is usually built for usual
|
|
||||||
# purposes. The format used here is whatever formats nixos-generators
|
|
||||||
# support.
|
|
||||||
images = {
|
|
||||||
bootstrap.format = "install-iso";
|
|
||||||
graphical-installer.format = "install-iso";
|
|
||||||
void.format = "vm";
|
|
||||||
};
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
# Exposes only my library with the custom functions to make it easier to
|
# Exposes only my library with the custom functions to make it easier to
|
||||||
@ -315,30 +273,32 @@
|
|||||||
|
|
||||||
# A list of NixOS configurations from the `./hosts` folder. It also has
|
# A list of NixOS configurations from the `./hosts` folder. It also has
|
||||||
# some sensible default configurations.
|
# some sensible default configurations.
|
||||||
nixosConfigurations = lib'.mapAttrsRecursive
|
nixosConfigurations = lib'.mapAttrs
|
||||||
(host: path:
|
(host: metadata:
|
||||||
let
|
let
|
||||||
host' = lib'.last host;
|
path = ./hosts/${host};
|
||||||
extraModules = [
|
extraModules = [
|
||||||
({ lib, ... }: {
|
({ lib, ... }: {
|
||||||
# We're very lax with setting the default since there's a lot
|
config = lib.mkMerge [
|
||||||
# of modules that may set this especially with image media
|
{ networking.hostName = metadata.hostname or host; }
|
||||||
# modules.
|
|
||||||
networking.hostName = host';
|
(lib.mkIf (metadata ? domain)
|
||||||
|
{ networking.domain = metadata.domain; })
|
||||||
|
];
|
||||||
})
|
})
|
||||||
(lib'.optionalAttrs (lib'.hasAttr host' images)
|
|
||||||
(
|
(lib'.optionalAttrs (metadata ? format)
|
||||||
let
|
inputs.nixos-generators.nixosModules."${metadata.format}")
|
||||||
imageFormat = images.${host'}.format;
|
|
||||||
in
|
|
||||||
inputs.nixos-generators.nixosModules.${imageFormat}
|
|
||||||
))
|
|
||||||
hostSharedConfig
|
hostSharedConfig
|
||||||
path
|
path
|
||||||
];
|
];
|
||||||
in
|
in
|
||||||
mkHost { inherit extraModules; })
|
mkHost {
|
||||||
(lib'.filesToAttr ./hosts);
|
inherit extraModules extraArgs;
|
||||||
|
system = metadata.system or defaultSystem;
|
||||||
|
})
|
||||||
|
images;
|
||||||
|
|
||||||
# We're going to make our custom modules available for our flake. Whether
|
# We're going to make our custom modules available for our flake. Whether
|
||||||
# or not this is a good thing is debatable, I just want to test it.
|
# or not this is a good thing is debatable, I just want to test it.
|
||||||
@ -366,7 +326,7 @@
|
|||||||
path
|
path
|
||||||
];
|
];
|
||||||
in
|
in
|
||||||
mkUser { inherit extraModules; })
|
mkUser { inherit extraModules extraArgs; })
|
||||||
(lib'.filesToAttr ./users/home-manager);
|
(lib'.filesToAttr ./users/home-manager);
|
||||||
|
|
||||||
# Extending home-manager with my custom modules, if anyone cares.
|
# Extending home-manager with my custom modules, if anyone cares.
|
||||||
@ -384,16 +344,16 @@
|
|||||||
in
|
in
|
||||||
inputs.flake-utils.lib.flattenTree (import ./pkgs { inherit pkgs; })
|
inputs.flake-utils.lib.flattenTree (import ./pkgs { inherit pkgs; })
|
||||||
// lib'.mapAttrs'
|
// lib'.mapAttrs'
|
||||||
(name: value:
|
(host: metadata:
|
||||||
lib'.nameValuePair "${name}-${value.format}" (mkImage {
|
lib'.nameValuePair "${host}-${metadata.format or "iso"}" (mkImage {
|
||||||
inherit system pkgs extraArgs;
|
inherit system pkgs extraArgs;
|
||||||
inherit (value) format;
|
format = metadata.format or "iso";
|
||||||
extraModules = [
|
extraModules = [
|
||||||
({ lib, ... }: {
|
({ lib, ... }: {
|
||||||
networking.hostName = lib.mkOverride 2000 name;
|
networking.hostName = lib.mkOverride 2000 host;
|
||||||
})
|
})
|
||||||
hostSharedConfig
|
hostSharedConfig
|
||||||
./hosts/${name}
|
./hosts/${host}
|
||||||
];
|
];
|
||||||
}))
|
}))
|
||||||
images);
|
images);
|
||||||
|
31
images.toml
Normal file
31
images.toml
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
# This is a custom data for this project where it lists the images found in
|
||||||
|
# this flake. This can range from NixOS configurations intended to be deployed
|
||||||
|
# for servers and desktops to installers.
|
||||||
|
#
|
||||||
|
# The data is then used for the image creation functions found in `flake.nix`.
|
||||||
|
# Each of the entry should correspond to one of the hosts in `./hosts/`
|
||||||
|
# directory.
|
||||||
|
#
|
||||||
|
# Take note, any images with the "iso" format is essentially made to be
|
||||||
|
# deployed somewhere (e.g., desktop, homelab server, VPS).
|
||||||
|
|
||||||
|
[ni]
|
||||||
|
system = "x86_64-linux"
|
||||||
|
format = "iso"
|
||||||
|
|
||||||
|
[plover]
|
||||||
|
system = "x86_64-linux"
|
||||||
|
format = "iso"
|
||||||
|
domain = "foodogsquared.one"
|
||||||
|
|
||||||
|
[void]
|
||||||
|
system = "x86_64-linux"
|
||||||
|
format = "vm"
|
||||||
|
|
||||||
|
[bootstrap]
|
||||||
|
system = "x86_64-linux"
|
||||||
|
format = "install-iso"
|
||||||
|
|
||||||
|
[graphical-installer]
|
||||||
|
system = "x86_64-linux"
|
||||||
|
format = "install-iso"
|
49
lib/images.nix
Normal file
49
lib/images.nix
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
# A set of functions intended for creating images. THis is meant to be imported
|
||||||
|
# for use in flake.nix and nowhere else.
|
||||||
|
{ inputs, lib }:
|
||||||
|
|
||||||
|
let
|
||||||
|
inherit (inputs) nixpkgs home-manager nixos-generators;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
# A wrapper around the NixOS configuration function.
|
||||||
|
mkHost = { system, extraModules ? [ ], extraArgs ? { } }:
|
||||||
|
(lib.makeOverridable inputs.nixpkgs.lib.nixosSystem) {
|
||||||
|
# The system of the NixOS system.
|
||||||
|
inherit system lib;
|
||||||
|
specialArgs = extraArgs;
|
||||||
|
modules =
|
||||||
|
# Append with our custom NixOS modules from the modules folder.
|
||||||
|
(lib.modulesToList (lib.filesToAttr ../modules/nixos))
|
||||||
|
|
||||||
|
# Our own modules.
|
||||||
|
++ extraModules;
|
||||||
|
};
|
||||||
|
|
||||||
|
# A wrapper around the home-manager configuration function.
|
||||||
|
mkUser = { system, extraModules ? [ ], extraArgs ? { } }:
|
||||||
|
inputs.home-manager.lib.homeManagerConfiguration {
|
||||||
|
inherit lib;
|
||||||
|
extraSpecialArgs = extraArgs;
|
||||||
|
pkgs = import nixpkgs { inherit system; };
|
||||||
|
modules =
|
||||||
|
# Importing our custom home-manager modules.
|
||||||
|
(lib.modulesToList (lib.filesToAttr ../modules/home-manager))
|
||||||
|
|
||||||
|
# Plus our own.
|
||||||
|
++ extraModules;
|
||||||
|
};
|
||||||
|
|
||||||
|
# A wrapper around the nixos-generators `nixosGenerate` function.
|
||||||
|
mkImage = { system ? null, pkgs ? null, extraModules ? [ ], extraArgs ? { }, format ? "iso" }:
|
||||||
|
inputs.nixos-generators.nixosGenerate {
|
||||||
|
inherit pkgs system format lib;
|
||||||
|
specialArgs = extraArgs;
|
||||||
|
modules =
|
||||||
|
# Import all of the NixOS modules.
|
||||||
|
(lib.modulesToList (lib.filesToAttr ../modules/nixos))
|
||||||
|
|
||||||
|
# Our own modules.
|
||||||
|
++ extraModules;
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user