Implement home-manager support

This commit is contained in:
Jack O'Sullivan 2022-02-13 23:06:31 +00:00
parent b5f5e7d38c
commit a30c45c01d
9 changed files with 176 additions and 38 deletions

View File

@ -1,15 +1,5 @@
{ lib, pkgs, ... }: { lib, pkgs, ... }:
{ {
fileSystems = {
"/persist" = {
device = "/dev/disk/by-label/persist";
fsType = "ext4";
neededForBoot = true;
};
};
networking = { };
my = { my = {
firewall = { firewall = {
trustedInterfaces = [ "blah" ]; trustedInterfaces = [ "blah" ];
@ -25,5 +15,21 @@
}; };
}; };
server.enable = true; server.enable = true;
homeConfig = {
programs = {
fish.enable = true;
};
};
}; };
fileSystems = {
"/persist" = {
device = "/dev/disk/by-label/persist";
fsType = "ext4";
neededForBoot = true;
};
};
networking = { };
} }

11
flake.lock generated
View File

@ -80,17 +80,16 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1644534280, "lastModified": 1644706973,
"narHash": "sha256-Gzf/Jq/F1vvTp6XkzPU+pBCj3OSAFLiR7f0ptwRseiI=", "narHash": "sha256-xOyxrhc5V79u0ZNmnPmJbY3ngtp43dNISEmrb8Ie6wQ=",
"owner": "nix-community", "owner": "nix-community",
"repo": "home-manager", "repo": "home-manager",
"rev": "6d9d9294d09b5e88df65f8c6651efb8a4d7d2476", "rev": "7c2ae0bdd20ddcaafe41ef669226a1df67f8aa06",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nix-community", "id": "home-manager",
"repo": "home-manager", "type": "indirect"
"type": "github"
} }
}, },
"impermanence": { "impermanence": {

View File

@ -13,7 +13,7 @@
agenix.inputs.nixpkgs.follows = "nixpkgs-unstable"; agenix.inputs.nixpkgs.follows = "nixpkgs-unstable";
deploy-rs.url = "github:serokell/deploy-rs"; deploy-rs.url = "github:serokell/deploy-rs";
deploy-rs.inputs.nixpkgs.follows = "nixpkgs-unstable"; deploy-rs.inputs.nixpkgs.follows = "nixpkgs-unstable";
home-manager.url = "github:nix-community/home-manager"; home-manager.url = "home-manager";
home-manager.inputs.nixpkgs.follows = "nixpkgs-unstable"; home-manager.inputs.nixpkgs.follows = "nixpkgs-unstable";
# Stuff used by systems # Stuff used by systems
@ -35,7 +35,7 @@
let let
inherit (builtins) mapAttrs attrValues; inherit (builtins) mapAttrs attrValues;
inherit (lib.flake) eachDefaultSystem; inherit (lib.flake) eachDefaultSystem;
inherit (lib.my) mkApp mkShellApp; inherit (lib.my) mkApp mkShellApp inlineModules mkDefaultSystemsPkgs flakePackageOverlay;
extendLib = lib: lib.extend (final: prev: { extendLib = lib: lib.extend (final: prev: {
my = import ./util.nix { lib = final; }; my = import ./util.nix { lib = final; };
@ -51,14 +51,24 @@
lib = pkgsFlakes.unstable.lib; lib = pkgsFlakes.unstable.lib;
pkgs' = mapAttrs pkgs' = mapAttrs
(_: path: lib.my.mkDefaultSystemsPkgs path { (_: path: mkDefaultSystemsPkgs path (system: {
overlays = [ overlays = [
libOverlay libOverlay
inputs.agenix.overlay inputs.agenix.overlay
inputs.deploy-rs.overlay inputs.deploy-rs.overlay
inputs.nix.overlay inputs.nix.overlay
(flakePackageOverlay inputs.home-manager system)
]; ];
}) }))
pkgsFlakes;
# Easiest to build the basic pkgs here (with our lib overlay too)
homePkgs' = mapAttrs
(_: path: mkDefaultSystemsPkgs path (_: {
overlays = [
libOverlay
];
}))
pkgsFlakes; pkgsFlakes;
modules = mapAttrs (_: f: ./. + "/modules/${f}") { modules = mapAttrs (_: f: ./. + "/modules/${f}") {
@ -69,23 +79,32 @@
firewall = "firewall.nix"; firewall = "firewall.nix";
server = "server.nix"; server = "server.nix";
}; };
homeModules = mapAttrs (_: f: ./. + "/home-modules/${f}") {
common = "common.nix";
};
in in
# Platform independent stuff # Platform independent stuff
{ {
lib = lib.my; lib = lib.my;
nixpkgs = pkgs'; nixpkgs = pkgs';
nixosModules = mapAttrs nixosModules = inlineModules modules;
(_: path: homeModules = inlineModules homeModules;
{
_file = path;
imports = [ (import path) ];
})
modules;
nixosConfigurations = import ./systems.nix { inherit lib pkgsFlakes inputs; modules = attrValues modules; }; nixosConfigurations = import ./systems.nix {
inherit lib pkgsFlakes inputs;
modules = attrValues modules;
homeModules = attrValues homeModules;
};
systems = mapAttrs (_: system: system.config.system.build.toplevel) self.nixosConfigurations; systems = mapAttrs (_: system: system.config.system.build.toplevel) self.nixosConfigurations;
vms = mapAttrs (_: system: system.config.my.build.devVM) self.nixosConfigurations; vms = mapAttrs (_: system: system.config.my.build.devVM) self.nixosConfigurations;
homeConfigurations = import ./homes.nix {
inherit lib inputs;
pkgs' = homePkgs';
modules = attrValues homeModules;
};
homes = mapAttrs(_: home: home.activationPackage) self.homeConfigurations;
} // } //
(eachDefaultSystem (system: (eachDefaultSystem (system:
let let
@ -99,7 +118,7 @@
}; };
devShell = pkgs.mkShell { devShell = pkgs.mkShell {
NIX_CONFIG = pkgs.writeText "nix.conf" NIX_USER_CONF_FILES = pkgs.writeText "nix.conf"
'' ''
experimental-features = nix-command flakes ca-derivations experimental-features = nix-command flakes ca-derivations
''; '';
@ -109,6 +128,7 @@
agenix agenix
deploy-rs.deploy-rs deploy-rs.deploy-rs
nixpkgs-fmt nixpkgs-fmt
home-manager
]; ];
}; };
})); }));

48
home-modules/common.nix Normal file
View File

@ -0,0 +1,48 @@
{ lib, pkgs, inputs, isStandalone, config, ... }:
let
inherit (lib) mkMerge mkIf mkDefault mkForce;
in
mkMerge [
{
programs = {
home-manager = {
# Even when enabled this will only be actually installed in standalone mode
enable = true;
};
htop = {
enable = true;
settings = {};
};
};
home = {
language.base = mkDefault "en_IE.UTF-8";
packages = with pkgs; [
tree
iperf3
];
# The flake passes a default setting, but we don't care about that
stateVersion = mkForce "22.05";
};
}
(mkIf isStandalone {
# Note: this only applies outside NixOS where home-manager imports nixpkgs internally
nixpkgs = {
overlays = [
inputs.nix.overlay
];
config = {
allowUnfree = true;
};
};
home = {
packages = with pkgs; [
nix
];
};
})
]

32
homes.nix Normal file
View File

@ -0,0 +1,32 @@
{ lib, inputs, pkgs', modules }:
let
inherit (builtins) removeAttrs mapAttrs;
inherit (lib) recursiveUpdate;
mkHome = name: {
system,
nixpkgs ? "unstable",
config,
...
}@args:
let
rest = removeAttrs args [ "nixpkgs" "config" ];
in
inputs.home-manager.lib.homeManagerConfiguration (recursiveUpdate rest {
configuration = config;
pkgs = pkgs'.${nixpkgs}.${system};
extraModules = modules ++ [{
_module.args = { inherit inputs; isStandalone = true; };
}];
});
in
mapAttrs mkHome {
"dev@castle" = {
system = "x86_64-linux";
nixpkgs = "unstable";
config = homes/castle.nix;
homeDirectory = "/home/dev";
username = "dev";
};
}

6
homes/castle.nix Normal file
View File

@ -0,0 +1,6 @@
{ pkgs, ... }:
{
programs = {
kakoune.enable = true;
};
}

View File

@ -1,5 +1,6 @@
{ lib, pkgs, inputs, system, config, options, ... }: { lib, pkgs, inputs, homeModules, config, options, ... }:
let let
inherit (builtins) attrValues;
inherit (lib) mkIf mkDefault mkAliasDefinitions; inherit (lib) mkIf mkDefault mkAliasDefinitions;
inherit (lib.my) mkOpt'; inherit (lib.my) mkOpt';
in in
@ -7,6 +8,7 @@ in
options.my = with lib.types; { options.my = with lib.types; {
# Pretty hacky but too lazy to figure out if there's a better way to alias the options # Pretty hacky but too lazy to figure out if there's a better way to alias the options
user = mkOpt' (attrsOf anything) { } "User definition (as `users.users.*`)."; user = mkOpt' (attrsOf anything) { } "User definition (as `users.users.*`).";
homeConfig = mkOpt' anything {} "Home configuration (as `home-manager.users.*`)";
}; };
config = config =
@ -25,13 +27,23 @@ in
}; };
}; };
time.timeZone = mkDefault "Europe/Dublin"; home-manager = {
useGlobalPkgs = mkDefault true;
useUserPackages = mkDefault true;
sharedModules = homeModules ++ [{
_module.args = { inherit inputs; isStandalone = false; };
}];
};
users = { users = {
mutableUsers = false; mutableUsers = false;
users.${uname} = mkAliasDefinitions options.my.user; users.${uname} = mkAliasDefinitions options.my.user;
}; };
# NOTE: As the "outermost" module is still being evaluated in NixOS land, special params (e.g. pkgs) won't be
# passed to it
home-manager.users.${uname} = config.my.homeConfig;
security = { security = {
sudo.enable = mkDefault false; sudo.enable = mkDefault false;
doas = { doas = {
@ -55,6 +67,8 @@ in
}; };
}; };
time.timeZone = mkDefault "Europe/Dublin";
boot = { boot = {
# Use latest LTS release by default # Use latest LTS release by default
kernelPackages = mkDefault pkgs.linuxKernel.packages.linux_5_15; kernelPackages = mkDefault pkgs.linuxKernel.packages.linux_5_15;
@ -80,10 +94,7 @@ in
environment.systemPackages = with pkgs; [ environment.systemPackages = with pkgs; [
bash-completion bash-completion
tree
vim vim
htop
iperf3
]; ];
services.openssh = { services.openssh = {

View File

@ -1,4 +1,4 @@
{ lib, pkgsFlakes, inputs, modules }: { lib, pkgsFlakes, inputs, modules, homeModules }:
let let
inherit (builtins) attrValues mapAttrs; inherit (builtins) attrValues mapAttrs;
inherit (lib) optionals mkDefault; inherit (lib) optionals mkDefault;
@ -35,7 +35,7 @@ let
] ++ (optionals docCustom modules); ] ++ (optionals docCustom modules);
modules = (optionals (!docCustom) modules) ++ [ modules = (optionals (!docCustom) modules) ++ [
{ {
_module.args = { inherit system inputs; }; _module.args = { inherit system inputs homeModules; };
system.name = name; system.name = name;
networking.hostName = mkDefault name; networking.hostName = mkDefault name;
} }

View File

@ -1,6 +1,6 @@
{ lib }: { lib }:
let let
inherit (builtins) replaceStrings elemAt; inherit (builtins) replaceStrings elemAt mapAttrs;
inherit (lib) genAttrs mapAttrs' types mkOption mkOverride; inherit (lib) genAttrs mapAttrs' types mkOption mkOverride;
inherit (lib.flake) defaultSystems; inherit (lib.flake) defaultSystems;
in in
@ -21,9 +21,25 @@ rec {
ports = checked (replaceStrings ["-"] [":"] (elemAt m 1)); ports = checked (replaceStrings ["-"] [":"] (elemAt m 1));
}; };
mkDefaultSystemsPkgs = path: args: genAttrs defaultSystems (system: import path (args // { inherit system; })); mkDefaultSystemsPkgs = path: args': genAttrs defaultSystems (system: import path ((args' system) // { inherit system; }));
mkApp = program: { type = "app"; inherit program; }; mkApp = program: { type = "app"; inherit program; };
mkShellApp = pkgs: name: text: mkApp (pkgs.writeShellScript name text).outPath; mkShellApp = pkgs: name: text: mkApp (pkgs.writeShellScript name text).outPath;
inlineModules = modules: mapAttrs
(_: path:
{
_file = path;
imports = [ (import path) ];
})
modules;
flakePackageOverlay' = flake: pkg: system: (final: prev:
let
pkg' = if pkg != null then flake.packages.${system}.${pkg} else flake.defaultPackage.${system};
name = if pkg != null then pkg else pkg'.name;
in
{
${name} = pkg';
});
flakePackageOverlay = flake: flakePackageOverlay' flake null;
mkOpt = type: default: mkOption { inherit type default; }; mkOpt = type: default: mkOption { inherit type default; };
mkOpt' = type: default: description: mkOption { inherit type default description; }; mkOpt' = type: default: description: mkOption { inherit type default description; };