refactor: migrate nixos configurations to flake-parts configuration structure

This commit is contained in:
Michael Thomas 2024-06-26 21:04:20 -04:00
parent 0c4d9ea95f
commit ee47690a8f
17 changed files with 441 additions and 187 deletions

23
common/default.nix Normal file
View File

@ -0,0 +1,23 @@
{
inputs,
config,
lib,
...
}: {
imports =
[
(lib.mkAliasOptionModule ["hm"] [
"home-manager"
"users"
config.my.user
])
]
++ lib.my.getModules [./.];
hm.imports = [./home.nix];
home-manager.extraSpecialArgs = {
inherit inputs;
};
home-manager.useGlobalPkgs = true;
}

25
common/home.nix Normal file
View File

@ -0,0 +1,25 @@
{
inputs,
lib,
pkgs,
...
}: {
imports = lib.my.getHmModules [./.];
# my = {
# difftastic.enable = true;
# direnv.enable = true;
# # helix.enable = true;
# git.enable = true;
# # gitui.enable = true;
# neovim.enable = true;
# yazi.enable = true;
# zsh.enable = true;
# };
# let standalone home-manager and home-manager in nixos/nix-darwin use the same derivation
home.packages = [
(pkgs.callPackage (inputs.home-manager + /home-manager) {path = inputs.home-manager;})
];
# home.stateVersion = "23.11";
}

26
common/my/default.nix Normal file
View File

@ -0,0 +1,26 @@
{lib, ...}: let
inherit (lib) mkOption types;
in {
options.my = {
user = mkOption {type = types.str;};
name = mkOption {type = types.str;};
email = mkOption {type = types.str;};
uid = mkOption {type = types.int;};
keys = mkOption {type = types.listOf types.singleLineStr;};
};
config = {
my = {
user = "michael";
name = "Michael Thomas";
email = "michaelhthomas@outlook.com";
uid = 1000;
keys = [
# Neptune
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDUYHiTel+RDzygCeNwV25cnBBNioM19EQWqxPC+xq7lNlNcAQ4wi9JIOONVGkshxPXzKZyR3F53Igs9JZr1E9088L52eUp35JhW1pthai82cw1jGkj9wxcKJnU6b7QNDKA+ejPTC/ciFFKytMyPgID0ICzBRQsnj15i4lGvGDgfTQ7qfI2J855H7S7qOpY7cbsGfeoz3f8Ye1FspJFDsNu9QhX05iunBcbni0uLoTtgS5cEBRr7H9RkKR7GCfd4Ae/sp+aeDkLU4aBkEi+A8VLfR74Juia1j/3mAAmkgJKGcBCKUv0ixBGd6XclZmkF8f2Hx0z8mDxJ7U8lsDUSKdvxK7fecsM4F/GTvjrUxV1nLD4SRQ06GtBmGQGDzIn3Nm1URd6gGT9W3486XJsqmGzjuHa5o4WhmBgh/Gs8xUcv7kWD5enrYacBr+HKVJWnnLFp+XfAYaMCT6j/4jc4D9/9hijRbhboICTDxWCWtFgz7QpsO6BQpi/wkR4Ql61vPc= michael@neptune"
# Oracle
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPc/HYsbpVsyoU2n0EiqQ4+3aSiFPtddjPCGK187W24f michael@oracle"
];
};
};
}

1
common/my/home.nix Symbolic link
View File

@ -0,0 +1 @@
default.nix

68
flake.lock generated
View File

@ -153,6 +153,24 @@
}
},
"flake-parts": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": {
"lastModified": 1719994518,
"narHash": "sha256-pQMhCCHyQGRzdfAkdJ4cIWiw+JNuWsTX7f0ZYSyz0VY=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "9227223f6d922fee3c7b190b2cc238a99527bbb7",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"flake-parts_2": {
"inputs": {
"nixpkgs-lib": [
"nixvim",
@ -540,6 +558,18 @@
"type": "github"
}
},
"nixpkgs-lib": {
"locked": {
"lastModified": 1719876945,
"narHash": "sha256-Fm2rDDs86sHy0/1jxTOKB1118Q0O3Uc7EC0iXvXKpbI=",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/5daf0514482af3f97abaefc78a6606365c9108e2.tar.gz"
},
"original": {
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/5daf0514482af3f97abaefc78a6606365c9108e2.tar.gz"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1719075281,
@ -603,11 +633,27 @@
"type": "github"
}
},
"nixpkgs_6": {
"locked": {
"lastModified": 1719690277,
"narHash": "sha256-0xSej1g7eP2kaUF+JQp8jdyNmpmCJKRpO12mKl/36Kc=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "2741b4b489b55df32afac57bc4bfd220e8bf617e",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixvim": {
"inputs": {
"devshell": "devshell",
"flake-compat": "flake-compat_2",
"flake-parts": "flake-parts",
"flake-parts": "flake-parts_2",
"git-hooks": "git-hooks",
"home-manager": "home-manager_3",
"nix-darwin": "nix-darwin",
@ -651,6 +697,7 @@
"agenix": "agenix",
"ags": "ags",
"darwin": "darwin_2",
"flake-parts": "flake-parts",
"home-manager": "home-manager_2",
"hyprland": "hyprland",
"nix-vscode-extensions": "nix-vscode-extensions",
@ -658,6 +705,7 @@
"nixvim": "nixvim",
"nur": "nur",
"rust-overlay": "rust-overlay",
"treefmt-nix": "treefmt-nix_2",
"unstable": "unstable",
"utils": "utils"
}
@ -776,6 +824,24 @@
"type": "github"
}
},
"treefmt-nix_2": {
"inputs": {
"nixpkgs": "nixpkgs_6"
},
"locked": {
"lastModified": 1719887753,
"narHash": "sha256-p0B2r98UtZzRDM5miGRafL4h7TwGRC4DII+XXHDHqek=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "bdb6355009562d8f9313d9460c0d3860f525bc6c",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "treefmt-nix",
"type": "github"
}
},
"unstable": {
"locked": {
"lastModified": 1719254875,

245
flake.nix
View File

@ -18,6 +18,8 @@
};
utils.url = "github:numtide/flake-utils";
flake-parts.url = "github:hercules-ci/flake-parts";
treefmt-nix.url = "github:numtide/treefmt-nix";
nixvim = {
url = "github:nix-community/nixvim/nixos-24.05";
inputs.nixpkgs.follows = "nixpkgs";
@ -32,197 +34,84 @@
};
};
outputs = {
outputs = inputs @ {
self,
nixpkgs,
unstable,
nur,
agenix,
utils,
rust-overlay,
nix-vscode-extensions,
darwin,
home-manager,
flake-parts,
...
} @ inputs: let
forAllSystems = nixpkgs.lib.genAttrs utils.lib.defaultSystems;
}:
flake-parts.lib.mkFlake {inherit inputs;} {
flake = let
baseHomeModule = {
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = {inherit inputs;};
};
in {
overlays = import ./overlays {inherit inputs;};
defaultModules = [
self.overlaysModule
home-manager.nixosModules.home-manager
./modules
];
darwinConfigurations = {
mac = darwin.lib.darwinSystem {
system = "aarch64-darwin";
specialArgs = {inherit inputs;};
modules = [
self.overlaysModule
home-manager.darwinModules.home-manager
baseHomeModule
{
home-manager.users.michael = import ./user/environments/mac/home.nix;
}
baseHomeModule = {
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = {inherit inputs;};
};
in {
# Your custom packages
# Accessible through 'nix build', 'nix shell', etc
packages = forAllSystems (system:
import ./pkgs {
pkgs = nixpkgs.legacyPackages.${system};
inherit inputs;
});
./modules/yabai.nix
./machines/mac/configuration.nix
];
};
};
overlays = import ./overlays {inherit inputs;};
homeConfigurations = {
nixos = inputs.home-manager.lib.homeManagerConfiguration {
system = "x86_64-linux";
homeDirectory = "/home/michael";
username = "michael";
stateVersion = "21.05";
configuration = {
config,
pkgs,
...
}: {
programs.home-manager.enable = true;
overlaysModule = {
nixpkgs.overlays = builtins.attrValues self.overlays;
};
nixosConfigurations = let
fullNixOSModules =
defaultModules
++ [
baseHomeModule
{
home-manager.users.michael = import ./user/environments/nixos/home.nix;
}
];
in {
kitchen = nixpkgs.lib.nixosSystem {
system = utils.lib.system.x86_64-linux;
modules =
fullNixOSModules
++ [
./modules/common.nix
./modules/containers.nix
./machines/kitchen/configuration.nix
];
specialArgs = {inherit inputs;};
};
thinkcentre = nixpkgs.lib.nixosSystem {
system = utils.lib.system.x86_64-linux;
modules =
fullNixOSModules
++ [
./modules/common.nix
./modules/hyprland.nix
./modules/containers.nix
./machines/thinkcentre/configuration.nix
agenix.nixosModules.default
{
environment.systemPackages = [
agenix.packages.x86_64-linux.default
imports = [
./user/environments/nixos/home.nix
];
}
];
specialArgs = {inherit inputs;};
};
};
};
terra = nixpkgs.lib.nixosSystem {
system = utils.lib.system.x86_64-linux;
modules =
fullNixOSModules
++ [
./modules/common.nix
./modules/hyprland.nix
./modules/containers.nix
./modules/applications/steam
mac = inputs.home-manager.lib.homeManagerConfiguration {
system = "aarch64-darwin";
homeDirectory = "/Users/michael";
username = "michael";
stateVersion = "22.05";
configuration = {
config,
pkgs,
...
}: {
programs.home-manager.enable = true;
./machines/terra/configuration.nix
agenix.nixosModules.default
{
environment.systemPackages = [
agenix.packages.x86_64-linux.default
imports = [
./user/environments/mac/home.nix
];
}
];
specialArgs = {inherit inputs;};
};
oracle = nixpkgs.lib.nixosSystem {
system = utils.lib.system.x86_64-linux;
modules =
defaultModules
++ [
./modules/nix.nix
baseHomeModule
{
home-manager.users.michael = import ./user/environments/nixos-server/home.nix;
}
agenix.nixosModules.default
{
environment.systemPackages = [
agenix.packages.x86_64-linux.default
];
}
./machines/oracle/configuration.nix
];
specialArgs = {inherit inputs;};
};
};
darwinConfigurations = {
mac = darwin.lib.darwinSystem {
system = "aarch64-darwin";
specialArgs = {inherit inputs;};
modules = [
self.overlaysModule
home-manager.darwinModules.home-manager
baseHomeModule
{
home-manager.users.michael = import ./user/environments/mac/home.nix;
}
./modules/yabai.nix
./machines/mac/configuration.nix
];
};
};
homeConfigurations = {
nixos = inputs.home-manager.lib.homeManagerConfiguration {
system = "x86_64-linux";
homeDirectory = "/home/michael";
username = "michael";
stateVersion = "21.05";
configuration = {
config,
pkgs,
...
}: {
programs.home-manager.enable = true;
imports = [
./user/environments/nixos/home.nix
];
};
};
mac = inputs.home-manager.lib.homeManagerConfiguration {
system = "aarch64-darwin";
homeDirectory = "/Users/michael";
username = "michael";
stateVersion = "22.05";
configuration = {
config,
pkgs,
...
}: {
programs.home-manager.enable = true;
imports = [
./user/environments/mac/home.nix
];
};
};
};
};
systems = [
"aarch64-darwin"
"aarch64-linux"
"x86_64-linux"
];
imports = [./flakes];
};
};
# // utils.lib.eachSystem [ "aarch64-linux" "x86_64-darwin" "x86_64-linux" ] (system: {
# nixpkgs = import nixpkgs {
# inherit system;
# overlays = [
# self.overlays.default
# ];
# config.allowUnfree = true;
# };
# });
}

10
flakes/default.nix Normal file
View File

@ -0,0 +1,10 @@
{
imports = [
./lib.nix
# ./darwin.nix
./nixos.nix
./packages.nix
# ./home-manager.nix
./treefmt.nix
];
}

30
flakes/lib.nix Normal file
View File

@ -0,0 +1,30 @@
{
perSystem = {
inputs',
config,
pkgs,
...
}: let
# use nixpkgs lib instead of flake-parts lib
# avoids home-manager warning about version mismatch
inherit (pkgs) lib;
in {
_module.args.lib = lib.extend (
final: prev: {
my = let
lib = final;
getPaths = file: root:
builtins.filter builtins.pathExists (
map (dir: root + "/${dir}/${file}") (
lib.attrNames (lib.filterAttrs (name: type: type == "directory") (builtins.readDir root))
)
);
in {
inherit getPaths;
getModules = builtins.concatMap (getPaths "default.nix");
getHmModules = builtins.concatMap (getPaths "home.nix");
};
}
);
};
}

118
flakes/nixos.nix Normal file
View File

@ -0,0 +1,118 @@
{
inputs,
self,
withSystem,
...
}: let
mkNixos = {
system ? "x86_64-linux",
nixpkgs ? inputs.nixpkgs,
config ? {},
overlays ? [],
modules ? [],
}:
withSystem system (
{
inputs',
lib,
pkgs,
system,
...
}: let
customPkgs = import nixpkgs (
lib.recursiveUpdate {
inherit system;
overlays = (builtins.attrValues self.overlays) ++ overlays;
config.allowUnfree = true;
} {inherit config;}
);
in
nixpkgs.lib.nixosSystem {
inherit system;
specialArgs = {
inherit inputs inputs' lib;
pkgs =
if (nixpkgs != inputs.nixpkgs || config != {} || overlays != [])
then customPkgs
else pkgs;
};
modules = [../nixos] ++ modules;
}
);
in {
flake.nixosConfigurations = let
defaultModules = [
../modules
inputs.agenix.nixosModules.default
{
environment.systemPackages = [
inputs.agenix.packages.x86_64-linux.default
];
}
];
baseHomeModule = {
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.extraSpecialArgs = {inherit inputs;};
};
fullNixOSModules =
defaultModules
++ [
baseHomeModule
{
home-manager.users.michael = import ../user/environments/nixos/home.nix;
}
];
in {
kitchen = mkNixos {
modules =
fullNixOSModules
++ [
../modules/common.nix
../modules/containers.nix
../machines/kitchen/configuration.nix
];
};
thinkcentre = mkNixos {
modules =
fullNixOSModules
++ [
../modules/common.nix
../modules/containers.nix
../machines/thinkcentre/configuration.nix
];
};
terra = mkNixos {
modules =
fullNixOSModules
++ [
../modules/common.nix
../modules/hyprland.nix
../modules/containers.nix
../modules/applications/libreoffice
../modules/applications/steam
../machines/terra/configuration.nix
];
};
oracle = mkNixos {
modules =
defaultModules
++ [
../modules/nix.nix
baseHomeModule
{
home-manager.users.michael = import ../user/environments/nixos-server/home.nix;
}
../machines/oracle/configuration.nix
];
};
};
}

23
flakes/packages.nix Normal file
View File

@ -0,0 +1,23 @@
{
self,
inputs,
...
}: {
perSystem = {
lib,
system,
...
}: let
pkgs = import inputs.nixpkgs {
inherit system;
config.allowUnfree = true;
overlays = builtins.attrValues self.overlays;
};
in {
_module.args.pkgs = pkgs;
packages = import ../pkgs {
inherit inputs pkgs;
};
};
}

8
flakes/treefmt.nix Normal file
View File

@ -0,0 +1,8 @@
{inputs, ...}: {
imports = [inputs.treefmt-nix.flakeModule];
perSystem.treefmt = {
projectRootFile = "flake.nix";
programs.alejandra.enable = true;
};
}

View File

@ -46,6 +46,8 @@
programs.zsh.enable = true;
my.hyprland.enable = true;
# Define a user account. Don't forget to set a password with passwd.
users.users.michael = {
isNormalUser = true;

View File

@ -47,6 +47,8 @@
programs.zsh.enable = true;
environment.variables.EDITOR = "nvim";
my.libreoffice.enable = true;
# Define a user account. Don't forget to set a password with passwd.
users.users.michael = {
isNormalUser = true;

View File

@ -114,6 +114,9 @@
firewallInterface = "wg0";
};
# TODO: Disable desktop
my.hyprland.enable = true;
my.services.forgejo = {
enable = true;
port = 3000;

View File

@ -1,8 +0,0 @@
{pkgs, ...}: {
environment.systemPackages = with pkgs; [
libreoffice-qt
hunspell
hunspellDicts.uk_UA
hunspellDicts.th_TH
];
}

14
nixos/default.nix Normal file
View File

@ -0,0 +1,14 @@
{
inputs,
lib,
...
}: {
imports =
[
inputs.home-manager.nixosModules.home-manager
../common
]
++ lib.my.getModules [./.];
hm.imports = lib.my.getHmModules [./.];
}

View File

@ -0,0 +1,22 @@
{
pkgs,
lib,
config,
...
}: let
inherit (lib) mkEnableOption mkIf;
cfg = config.my.libreoffice;
in {
options.my.libreoffice = {
enable = mkEnableOption "libreoffice";
};
config = mkIf cfg.enable {
environment.systemPackages = with pkgs; [
libreoffice-qt
hunspell
hunspellDicts.uk_UA
hunspellDicts.th_TH
];
};
}