feat(flake): implement configuration loading

This commit is contained in:
514fpv 2024-01-02 14:42:27 +08:00
parent b4944b7383
commit 61db72b9ab
Signed by: koishi
SSH key fingerprint: SHA256:axz0uIzzY+5W19i7QOUuiw5LSqhKfCBKPf3L4xFRxLw
6 changed files with 213 additions and 0 deletions

7
faucet/default.nix Normal file
View file

@ -0,0 +1,7 @@
{ lib, ... }: {
imports = lib.pipe ./. [
builtins.readDir
(lib.filterAttrs (n: ty: ty == "directory" && builtins.pathExists ./${n}/default.nix))
(lib.mapAttrsToList (n: _: ./${n}))
];
}

148
home/profile.nix Normal file
View file

@ -0,0 +1,148 @@
{ pkgs
, lib
, config
, ... }: with lib; let
cfg = config.users;
in {
options.users = {
profiles = mkOption {
type = with types; attrsOf (submodule {
options = {
uid = mkOption {
type = with types; nullOr int;
default = null;
description = "uid passthrough to base user configuration";
};
admin = mkOption {
type = with types; bool;
default = false;
description = "add user to privileged groups";
};
sshLogin = mkOption {
type = with types; bool;
default = false;
description = "enable ssh authorized keys for user";
};
};
});
description = "preconfigured users with profile options";
};
adminGroups = mkOption {
type = with types; listOf str;
description = "groups to add privileged users to";
};
homeModules = mkOption {
type = with types; listOf anything;
description = "home manager modules imported into every profile";
};
home = {
size = mkOption {
type = with types; str;
default = "1G";
description = "default home tmpfs size, mounted to prevent accidentally filling up root";
};
persist = {
files = mkOption {
type = with types; listOf (oneOf [ str (attrsOf str) ]);
default = [ ];
};
directories = mkOption {
type = with types; listOf (oneOf [ str (attrsOf str) ]);
default = [ ];
};
};
};
};
config = {
users = {
users = mapAttrs (name: opts: {
inherit (opts) uid;
extraGroups = mkIf opts.admin cfg.adminGroups;
openssh.authorizedKeys.keys = mkIf (opts.sshLogin && config.services.openssh.enable)
config.faucet.auth.openssh.publicKeys;
hashedPasswordFile = "/nix/persist/shadow/${name}";
shell = pkgs.zsh;
isNormalUser = mkIf (name != "root") true;
}) cfg.profiles;
mutableUsers = false;
# base groups
adminGroups = [
"wheel" "dialout" "kvm"
"systemd-journal"
];
# base home modules in current directory
homeModules = pipe ./. [
builtins.readDir
(filterAttrs (n: ty: ty == "directory" && builtins.pathExists ./${n}/home.nix))
(mapAttrsToList (n: _: ./${n}/home.nix))
] ++ [ {
options.passthrough = mkOption {
type = with types; attrsOf anything;
description = "passthrough values from nixos configuration";
};
} ];
# basic persistence
home.persist = {
directories = [
"src"
{ directory = ".gnupg"; mode = "0700"; }
{ directory = ".ssh"; mode = "0700"; }
{ directory = ".local/share/keyrings"; mode = "0700"; }
];
};
};
# mount tmpfs on each user's home directory with appropriate ownership
fileSystems = mapAttrs'
(name: opts: nameValuePair
# nixpkgs quirk: accessing user configuration here causes infinite recursion
# this workaround ensures proper home directory path unless overridden elsewhere
(if name != "root" then "/home/${name}" else "/root") {
device = "homefs";
fsType = "tmpfs";
options = [ "size=${cfg.home.size}"
"uid=${builtins.toString opts.uid}"
"gid=${builtins.toString cfg.groups.${cfg.users.${name}.group}.gid}"
"mode=700" ];
# impermanence sets permissions before filesystems are mounted
# this mounts filesystem in initrd therefore working around that bug
neededForBoot = true;
}) cfg.profiles;
home-manager.users = mapAttrs (name: opts: {
imports = cfg.homeModules;
home.stateVersion = "23.11";
}) cfg.profiles;
# set up standard persistence for users
# this is registered internally for each software's configuration
environment.persistence."/nix/persist" = {
users = mapAttrs (name: _: cfg.home.persist // {
# root workaround, ugly but necessary
# cannot get it properly for the same reason
# mentioned above in fileSystems
home = mkIf (name == "root") "/root";
}) cfg.profiles;
hideMounts = true;
};
# enable passwordless sudo
security.sudo.wheelNeedsPassword = false;
};
# this is for home components that need to extend nixos
imports = pipe ./. [
builtins.readDir
(filterAttrs (n: ty: ty == "directory" && builtins.pathExists ./${n}/nixos.nix))
(mapAttrsToList (n: _: ./${n}/nixos.nix))
];
}

7
home/user.nix Normal file
View file

@ -0,0 +1,7 @@
{
users.profiles = {
koishi = { uid = 1300; admin = true; sshLogin = true; }; # 0x514 = 1300 :)
staging.uid = 1000;
root.uid = 0;
};
}

15
spec/channel.nix Normal file
View file

@ -0,0 +1,15 @@
{ inputs, ... }: with inputs; with nixpkgs.lib; let
mapInputs = fn: map fn (lists.remove "self" (attrNames inputs));
channelPath = "/etc/nix/channels";
in {
nix = {
nixPath = mapInputs (i: "${i}=${channelPath}/${i}");
registry = listToAttrs
(mapInputs (name: {
inherit name;
value = {flake = inputs.${name};};
}));
};
systemd.tmpfiles.rules = mapInputs (i: "L+ ${channelPath}/${i} - - - - ${inputs.${i}.outPath}");
}

7
spec/constant.nix Normal file
View file

@ -0,0 +1,7 @@
{
i18n.defaultLocale = "en_GB.UTF-8";
time.timeZone = "Asia/Hong_Kong";
environment.etc.nixos.source = "/nix/persist/config";
nix.settings.experimental-features = [ "nix-command" "flakes" ];
system.stateVersion = "23.11";
}

29
spec/default.nix Normal file
View file

@ -0,0 +1,29 @@
{ inputs, ... }: with inputs; with nixpkgs.lib; {
flake.nixosConfigurations = (lists.foldr (name: spec: spec // {
${name} = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
specialArgs = inputs // { inherit inputs; };
modules = [
../faucet
../home/profile.nix
../home/user.nix
./constant.nix
./channel.nix
impermanence.nixosModules.impermanence
home-manager.nixosModules.home-manager
{
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
}
lanzaboote.nixosModules.lanzaboote
./${name}
{ networking.hostName = name; }
];
};
}) { }) (pipe ./. [
builtins.readDir
(filterAttrs (n: ty: ty == "directory" && builtins.pathExists ./${n}/default.nix))
(mapAttrsToList (n: _: n))
]);
}