Skip to content

Commit

Permalink
Simplify lib setup
Browse files Browse the repository at this point in the history
  • Loading branch information
KubqoA committed Mar 1, 2025
1 parent 7003ca5 commit 9b67040
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 164 deletions.
82 changes: 43 additions & 39 deletions lib/default.nix → bootstrap.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
# Turns the abstract per system configuration in ‹flake.nix› to individual
# nixos, darwin and home-manager configs. Additionally for each system architecture
# defines formatting with ‹nix fmt› and a custom ‹nix develop› shell with helpers
# for activating configurations on new machines. Also defines a custom lib with
# extensions defined in ‹lib.nix›.
#
# For all nixos, darwin, and home-manager config autoloads ‹config.nix›, agenix, and
# all autoloaded modules.
#
# Usage:
# ```
# import ./bootstrap.nix inputs {
# <architecture> = {
# homes.<name> = path;
# systems.<name> = path;
# };
# }
# ```
inputs @ {
agenix,
home-manager,
nix-darwin,
nixpkgs,
self,
...
}: systems: let
inherit ((import nixpkgs {system = "x86_64-linux";}).lib) foldl mapAttrsToList recursiveUpdate;
# system agnostic lib with custom extensions
lib = nixpkgs.lib.extend (import ./lib.nix inputs);

inherit (lib) foldl recursiveUpdate mapAttrsToList;

mapSystem = system: {
homes ? {},
Expand All @@ -17,30 +36,16 @@ inputs @ {
config.allowUnfree = true;
};

lib = pkgs.lib.extend (lib: super: let
# Automatically detect all .nix files in lib/ directory, excluding default.nix
libs =
builtins.filter
(path: path != "default.nix" && lib.strings.hasSuffix ".nix" path)
(builtins.attrNames (builtins.readDir ./.));
importLib = path: import ./${path} {inherit inputs lib pkgs system;};
in {
# Expose custom lib extensions via `_.` prefix
_ = lib.foldr (a: b: a // b) {} (map importLib libs);
# Make sure to keep library extensions from home-manager
hm = home-manager.lib.hm;
});

systemSpecifics =
if system == "aarch64-darwin" || system == "x86_64-darwin"
if pkgs.stdenv.isDarwin
then {
fn = nix-darwin.lib.darwinSystem;
fn = lib.darwinSystem;
option = "darwinConfigurations";
command = "nix run nix-darwin --";
agenixModule = agenix.darwinModules.default;
}
else {
fn = nixpkgs.lib.nixosSystem;
fn = lib.nixosSystem;
option = "nixosConfigurations";
command = "nixos-rebuild";
agenixModule = agenix.nixosModules.default;
Expand All @@ -52,35 +57,31 @@ inputs @ {
specialArgs = {inherit inputs lib pkgs self system osName;};
modules =
[
../config.nix
./config.nix
systemSpecifics.agenixModule
{networking.hostName = lib.mkDefault osName;}
path
]
++ lib._.autoloadedModules;
++ lib.autoloadedModules;
});

mapHomes = builtins.mapAttrs (homeName: path:
home-manager.lib.homeManagerConfiguration {
lib.homeManagerConfiguration {
inherit pkgs;
extraSpecialArgs = {inherit inputs lib system homeName;};
modules =
[
../config.nix
./config.nix
agenix.homeManagerModules.default
path
]
++ lib._.autoloadedModules;
++ lib.autoloadedModules;
});
in {
formatter.${system} = pkgs.alejandra;
devShells.${system}.default = pkgs.mkShell {
packages = [pkgs.alejandra pkgs.home-manager];
shellHook = ''
# Set custom prompt colors using ANSI escape codes
# \[\e[1;32m\] - Bold Green
# \[\e[1;34m\] - Bold Blue
# \[\e[0m\] - Reset formatting
export PS1='\[\e[1;32m\][${system}:\w]\$\[\e[0m\] '
echo
echo "‹os›: ${builtins.concatStringsSep ", " (builtins.attrNames hosts)}"
Expand All @@ -100,15 +101,18 @@ inputs @ {
homeConfigurations = mapHomes homes;
};

mappedSystems = foldl recursiveUpdate {} (mapAttrsToList mapSystem systems);
configuration = foldl recursiveUpdate {} (mapAttrsToList mapSystem systems);

# Merge all options into one attribute set for use with ‹nixd›
options = let
getOptions = configs: foldl recursiveUpdate {} (mapAttrsToList (_: value: value.options) configs);
in {
nixos = getOptions mappedSystems.nixosConfigurations;
darwin = getOptions mappedSystems.darwinConfigurations;
home-manager = getOptions mappedSystems.homeConfigurations;
};
getOptions = configs: foldl recursiveUpdate {} (mapAttrsToList (_: value: value.options) configs);
in
mappedSystems // {inherit options;}
configuration
// {
inherit lib;

# Merge all options into one attribute set for use with ‹nixd›
options = {
nixos = getOptions self.nixosConfigurations;
darwin = getOptions self.darwinConfigurations;
home-manager = getOptions self.homeConfigurations;
};
}
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
};

outputs = inputs:
import ./lib inputs {
import ./bootstrap.nix inputs {
aarch64-darwin = {
homes.jakub-macos = ./homes/jakub-macos;
hosts.nyckelharpa = ./hosts/nyckelharpa;
Expand Down
2 changes: 1 addition & 1 deletion homes/jakub-macos/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pkgs,
...
}: {
imports = lib._.moduleImports [
imports = lib.moduleImports [
"common/aliases"
"common/env"
"common/ghostty"
Expand Down
2 changes: 1 addition & 1 deletion homes/jakub-nixos/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pkgs,
...
}: {
imports = lib._.moduleImports [
imports = lib.moduleImports [
"common/aliases"
"common/env"
"common/git"
Expand Down
2 changes: 1 addition & 1 deletion homes/jakub-server/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{lib, ...}: {
imports = lib._.moduleImports [
imports = lib.moduleImports [
"common/aliases"
"common/env"
"common/git"
Expand Down
2 changes: 1 addition & 1 deletion hosts/harmonium/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
inputs.nixos-hardware.nixosModules.lenovo-thinkpad-p14s-amd-gen2
./hardware-configuration.nix
]
++ lib._.moduleImports [
++ lib.moduleImports [
"common/nix"
"common/packages"
];
Expand Down
2 changes: 1 addition & 1 deletion hosts/nyckelharpa/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
./homebrew.nix
./system.nix
]
++ lib._.moduleImports [
++ lib.moduleImports [
"common/nix"
"common/packages"
"darwin/icons"
Expand Down
7 changes: 4 additions & 3 deletions hosts/organ/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
./syncthing.nix
./users.nix
]
++ lib._.moduleImports [
++ lib.moduleImports [
"common/nix"
"common/packages"
"server/dns"
"server/seafile"
"server/tailscale"
];

age.secrets = lib._.defineSecrets ["organ-tailscale-auth-key"] {
"organ-seafile-password" = {
age.secrets = lib.defineSecrets {
organ-tailscale-auth-key = {};
organ-seafile-password = {
owner = config.services.seafile.user;
mode = "0600";
};
Expand Down
2 changes: 1 addition & 1 deletion hosts/organ/mail.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
in {
imports = [inputs.simple-nixos-mailserver.nixosModule];

age.secrets = lib._.defineSecrets ["organ-sasl-passwd"] {};
age.secrets = lib.defineSecrets {organ-sasl-passwd = {};};

mailserver = {
enable = true;
Expand Down
2 changes: 1 addition & 1 deletion hosts/organ/users.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pkgs,
...
}: {
age.secrets = lib._.defineSecrets ["organ-jakub-password-hash"] {};
age.secrets = lib.defineSecrets {organ-jakub-password-hash = {};};

users.users = {
${config.username} = {
Expand Down
40 changes: 40 additions & 0 deletions lib.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
inputs: lib: _:
{
autoloadedModules = let
optionsDir = ./modules/autoload;
nixFiles = lib.filterAttrs (n: _: lib.hasSuffix ".nix" n) (builtins.readDir optionsDir);
in
map (file: optionsDir + "/${file}") (builtins.attrNames nixFiles);

# Helper to easily import modules in home/system configs
moduleImports = let
modulePath = path:
if builtins.pathExists ./modules/${path}
then ./modules/${path}
else ./modules/${path}.nix;
in
builtins.map modulePath;

# On macOS creates a simple package that symlinks to a package installed by homebrew
brew-alias = pkgs: name:
lib.mkIf pkgs.stdenv.isDarwin
(pkgs.stdenv.mkDerivation {
name = "${name}-brew";
version = "1.0.0";
dontUnpack = true;
installPhase = ''
mkdir -p $out/bin
ln -s /opt/homebrew/bin/${name} $out/bin/${name}
'';
meta = with pkgs.lib; {
mainProgram = "${name}";
description = "Wrapper for Homebrew-installed ${name}";
platforms = platforms.darwin;
};
});

defineSecrets = secrets: lib.mapAttrs (name: options: {file = ../secrets + "/${name}.age";} // options) secrets;
}
# Make sure to add lib extensions from inputs
// inputs.home-manager.lib
// inputs.nix-darwin.lib
64 changes: 0 additions & 64 deletions lib/README.md

This file was deleted.

7 changes: 0 additions & 7 deletions lib/autoload.nix

This file was deleted.

23 changes: 0 additions & 23 deletions lib/brew-alias.nix

This file was deleted.

9 changes: 0 additions & 9 deletions lib/modules.nix

This file was deleted.

7 changes: 0 additions & 7 deletions lib/secrets.nix

This file was deleted.

Loading

0 comments on commit 9b67040

Please sign in to comment.