Skip to content

Commit

Permalink
Split flake in multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
billy4479 committed Dec 29, 2024
1 parent e4c8db9 commit 2152b61
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 163 deletions.
171 changes: 8 additions & 163 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -61,188 +61,33 @@
outputs =
{
nixpkgs,
home-manager,
catppuccin,
nix-vscode-extensions,
catppuccin-vsc,
plasma-manager,
spicetify-nix,
server-tool,
sops-nix,
...
}@inputs:
let
system = "x86_64-linux";

# Shortcuts
pkgs = import nixpkgs {
config.allowUnfree = true;
inherit system;
};

lib = nixpkgs.lib;

my-packages = import ./packages { inherit pkgs; } // {
server-tool = server-tool.packages.${system}.default;
};

user = {
username = "billy";
fullName = "Billy Panciotto";
};

# https://github.com/catppuccin/nix/blob/5501cb508c2d4224d932a0b924d75454b68680bf/modules/lib/default.nix#L79
mkUpper =
str:
(lib.toUpper (builtins.substring 0 1 str)) + (builtins.substring 1 (builtins.stringLength str) str);

mkCatppuccinColors =
{ flavor, accent }:
rec {
inherit flavor accent;
flavorWithAccent = if flavor == "frappe" then "Frappé" else flavor;

upper = {
flavor = mkUpper flavor;
accent = mkUpper accent;
flavorWithAccent = mkUpper flavorWithAccent;
};
};

# WARNING: the following part might look like a mess but it's actually quite straight forward.
# We have all this code because we want to share the same `specialArgs`/`extraSpecialArgs` between
# the nixos config and the home-manager config.

# First we define the specialArgs/extraSpecialArgs for nixos config and home-manager
extraArgs = {
extraConfig = {
desktop = "kde";
wayland = true;
bluetooth = true;
games = false;
isServer = false;
standaloneHomeManager = true;

catppuccinColors = mkCatppuccinColors {
flavor = "frappe";
accent = "green";
};

inherit user;
};

extraPkgs = {
vscode-extensions = nix-vscode-extensions.extensions.${system};
spicetifyPkgs = spicetify-nix.legacyPackages.${system};
inherit catppuccin-vsc my-packages;
};

flakeInputs = inputs;
hosts = import ./flake/system.nix {
inherit
pkgs
system
my-packages
inputs
;
};

# This function creates the flake output for a single host:
# we take a `hostname`, `extraSystemModules` (that we pass to NixOS),
# and `args` (that we pass to both to NixOS and home-manager).
hostFn =
{
hostname,
extraSystemModules ? [ ],
args ? { },
}:
let
specialArgs = lib.recursiveUpdate extraArgs { extraConfig = (args // { inherit hostname; }); };
defaultHomeManagerModules = [
catppuccin.homeManagerModules.catppuccin
plasma-manager.homeManagerModules.plasma-manager
spicetify-nix.homeManagerModules.default
sops-nix.homeManagerModules.sops
./user
];
in
{
nixosConfigurations.${hostname} = nixpkgs.lib.nixosSystem {
inherit system specialArgs;
modules =
[
./system
sops-nix.nixosModules.sops
]
++ lib.optionals (!specialArgs.extraConfig.standaloneHomeManager) [
home-manager.nixosModules.home-manager
{
home-manager = {
useGlobalPkgs = true;
useUserPackages = true;
extraSpecialArgs = specialArgs;
users.${user.username}.imports = defaultHomeManagerModules;
};
}
]
++ extraSystemModules;
};

homeConfigurations =
if specialArgs.extraConfig.standaloneHomeManager then
{
"${user.username}@${hostname}" = home-manager.lib.homeManagerConfiguration {
inherit pkgs;
extraSpecialArgs = specialArgs;
modules = defaultHomeManagerModules;
};
}
else
{ };
};

# This function is like `lib.recursiveUpdate` but takes a list instead.
recursiveMerge = attrList: builtins.foldl' (a: b: lib.recursiveUpdate a b) { } attrList;

# Now we map `hostProps` to "real" configurations using `hostFn` and we merge them.
createAndMergeHosts = hostProps: recursiveMerge (map hostFn hostProps);
in
{
formatter.${system} = pkgs.nixfmt-rfc-style;
}
# Finally we define our `hostProps`:
# these are the real configuration changes I want from one host to another.
// createAndMergeHosts [
{
hostname = "nixbox";
args = {
bluetooth = false;
};
extraSystemModules = [
./system/hosts/vm
];
}
{
hostname = "portatilo";
args = { };
extraSystemModules = [
./system/hosts/portatilo
];
}
{
hostname = "computerone";
args = {
desktop = "qtile";
wayland = false;
games = true;
};
extraSystemModules = [
./system/hosts/computerone
];
}
{
hostname = "serverone";
args = {
isServer = true;
standaloneHomeManager = false;
bluetooth = false;
};
extraSystemModules = [
./system/hosts/serverone
];
}
];
// hosts;
}
107 changes: 107 additions & 0 deletions flake/createAndMergeHosts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
pkgs,
system,
my-packages,
inputs,
}:
let
lib = inputs.nixpkgs.lib;

# https://github.com/catppuccin/nix/blob/5501cb508c2d4224d932a0b924d75454b68680bf/modules/lib/default.nix#L79
mkUpper =
str:
(lib.toUpper (builtins.substring 0 1 str)) + (builtins.substring 1 (builtins.stringLength str) str);

mkCatppuccinColors =
{ flavor, accent }:
rec {
inherit flavor accent;
flavorWithAccent = if flavor == "frappe" then "Frappé" else flavor;

upper = {
flavor = mkUpper flavor;
accent = mkUpper accent;
flavorWithAccent = mkUpper flavorWithAccent;
};
};

# This function creates the flake output for a single host:
# we take a `hostname`, `extraSystemModules` (that we pass to NixOS),
# and `args` (that we pass to both to NixOS and home-manager).
hostFn =
defaultOptions:
{
hostname,
extraSystemModules ? [ ],
args,
}:
let
defaultedArgs = lib.recursiveUpdate defaultOptions args;
user = defaultedArgs.user;

specialArgs = {
extraConfig = builtins.removeAttrs (lib.recursiveUpdate defaultedArgs {
catppuccinColors = mkCatppuccinColors defaultedArgs.catppuccin;
inherit hostname;
}) [ "catppuccin" ];

extraPkgs = {
vscode-extensions = inputs.nix-vscode-extensions.extensions.${system};
spicetifyPkgs = inputs.spicetify-nix.legacyPackages.${system};
inherit (inputs) catppuccin-vsc;
inherit my-packages;
};
flakeInputs = inputs;
};

defaultHomeManagerModules = [
inputs.catppuccin.homeManagerModules.catppuccin
inputs.plasma-manager.homeManagerModules.plasma-manager
inputs.spicetify-nix.homeManagerModules.default
inputs.sops-nix.homeManagerModules.sops
../user
];
in
{
nixosConfigurations.${hostname} = inputs.nixpkgs.lib.nixosSystem {
inherit system specialArgs;
modules =
[
../system
inputs.sops-nix.nixosModules.sops
]
++ lib.optionals (!specialArgs.extraConfig.standaloneHomeManager) [
inputs.home-manager.nixosModules.home-manager
{
home-manager = {
useGlobalPkgs = true;
useUserPackages = true;
extraSpecialArgs = specialArgs;
users.${user.username}.imports = defaultHomeManagerModules;
};
}
]
++ extraSystemModules;
};

homeConfigurations =
if specialArgs.extraConfig.standaloneHomeManager then
{
"${user.username}@${hostname}" = inputs.home-manager.lib.homeManagerConfiguration {
inherit pkgs;
extraSpecialArgs = specialArgs;
modules = defaultHomeManagerModules;
};
}
else
{ };
};

# This function is like `lib.recursiveUpdate` but takes a list instead.
recursiveMerge = attrList: builtins.foldl' (a: b: lib.recursiveUpdate a b) { } attrList;

# Now we map `hostProps` to "real" configurations using `hostFn` and we merge them.
createAndMergeHosts =
defaultOptions: hostProps: recursiveMerge (map (prop: hostFn defaultOptions prop) hostProps);
in
createAndMergeHosts
68 changes: 68 additions & 0 deletions flake/system.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
pkgs,
system,
my-packages,
inputs,
}@args:
let
defaultOptions = {
desktop = "kde";
wayland = true;
bluetooth = true;
games = false;
isServer = false;
standaloneHomeManager = true;

catppuccin = {
flavor = "frappe";
accent = "green";
};

user = {
username = "billy";
fullName = "Billy Panciotto";
};
};

createAndMergeHosts = import ./createAndMergeHosts.nix args;
in
createAndMergeHosts defaultOptions [
{
hostname = "nixbox";
args = {
bluetooth = false;
};
extraSystemModules = [
../system/hosts/vm
];
}
{
hostname = "portatilo";
args = { };
extraSystemModules = [
../system/hosts/portatilo
];
}
{
hostname = "computerone";
args = {
desktop = "qtile";
wayland = false;
games = true;
};
extraSystemModules = [
../system/hosts/computerone
];
}
{
hostname = "serverone";
args = {
isServer = true;
standaloneHomeManager = false;
bluetooth = false;
};
extraSystemModules = [
../system/hosts/serverone
];
}
]

0 comments on commit 2152b61

Please sign in to comment.