rework lib and some general changes

This commit is contained in:
Reid 2024-02-27 23:10:03 -08:00
parent 48b7ab9fab
commit af804c63f8
11 changed files with 152 additions and 121 deletions

View file

@ -1,11 +1,13 @@
{ config, inputs, lib, pkgs, ... }:
let
inherit (lib) filterAttrs _;
inherit (builtins) toString;
inherit (lib.modules) mkDefault;
inherit (lib.my) mapModulesRec';
in {
imports =
[ inputs.home-manager.nixosModules.home-manager ]
++ _.mapModulesRec' ./modules import;
++ (mapModulesRec' (toString ./modules) import);
nix = {
settings = {
@ -25,14 +27,15 @@ in {
environment.systemPackages = with pkgs; [
unrar unzip
curl wget
# hello! if you remove this, good luck
# ever rebuilding your system using flakes!
# nix does not work without git.
# do not remove this.
# nix is awesome
git
];
time.timeZone = lib.mkDefault "America/Los_Angeles";
time.timeZone = mkDefault "America/Los_Angeles";
i18n.defaultLocale = lib.mkDefault "en_US.UTF-8";
i18n.defaultLocale = mkDefault "en_US.UTF-8";
system.stateVersion = lib.mkDefault "23.11";
system.stateVersion = mkDefault "23.11";
}

22
flake.lock generated
View file

@ -2,7 +2,9 @@
"nodes": {
"home-manager": {
"inputs": {
"nixpkgs": "nixpkgs"
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1705535278,
@ -19,22 +21,6 @@
}
},
"nixpkgs": {
"locked": {
"lastModified": 1705316053,
"narHash": "sha256-J2Ey5mPFT8gdfL2XC0JTZvKaBw/b2pnyudEXFvl+dQM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "c3e128f3c0ecc1fb04aef9f72b3dcc2f6cecf370",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1705496572,
"narHash": "sha256-rPIe9G5EBLXdBdn9ilGc0nq082lzQd0xGGe092R/5QE=",
@ -53,7 +39,7 @@
"root": {
"inputs": {
"home-manager": "home-manager",
"nixpkgs": "nixpkgs_2"
"nixpkgs": "nixpkgs"
}
}
},

View file

@ -5,29 +5,44 @@
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
home-manager.url = "github:nix-community/home-manager";
home-manager.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = inputs @ { self, nixpkgs, ... }:
let
inherit (lib.my) mapModules mapModulesRec mkHost;
system = "aarch64-linux";
lib = import ./lib { inherit pkgs inputs; lib = nixpkgs.lib; };
inherit (lib._) mapModules mapModulesRec mkHost;
mkPkgs = pkgs: overlays: import pkgs {
mkPkgs = pkgs: extraOverlays:
import pkgs {
inherit system;
config.allowUnfree = true;
overlays = overlays ++ (lib.attrValues self.overlays);
config.allowAliases = false;
overlays = extraOverlays ++ (lib.attrValues self.overlays);
};
pkgs = mkPkgs nixpkgs [ self.overlays.default ];
lib = nixpkgs.lib.extend (final: prev: {
my = import ./lib {
inherit pkgs inputs;
lib = final;
};
});
in {
lib = lib.my;
overlays =
(mapModules ./overlays import)
// {
default = final: prev: {
my = self.packages.${system};
};
};
pkgs = mkPkgs nixpkgs [ self.overlay ];
in {
packages."${system}" = mapModules ./packages (p: pkgs.callPackage p {});
overlay = final: prev: {
_ = self.packages."${system}";
};
overlays = mapModules ./overlays import;
nixosModules = (mapModulesRec ./modules import);
nixosModules = mapModulesRec ./modules import;
nixosConfigurations = mapModules ./hosts (host: mkHost host { inherit system; });
};
}

View file

@ -1,10 +0,0 @@
[
# reidlab
{ hostname = "reidlab@rei-pc";
ssh = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICmwWuwS+a1GzYFSNOkgk/zF5bolXqat1RP5FXJv+vto reidlab@rei-pc";
}
{
hostname = "reidlab@rei-phone";
ssh = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKC12NkyZAFNDHfq1ECh4uAgM4mpKfsQnL3XF/ZzSyCJ reidlab@rei-phone";
}
]

View file

@ -17,9 +17,9 @@ in {
conf = {
packages = with pkgs; [ bat tree micro duf ];
extraGroups = [ "wheel" "dotfiles" ];
openssh.authorizedKeys.keys = fetchSSHKeys [
"reidlab@rei-pc"
"reidlab@rei-phone"
openssh.authorizedKeys.keys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICmwWuwS+a1GzYFSNOkgk/zF5bolXqat1RP5FXJv+vto reidlab@rei-pc"
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKC12NkyZAFNDHfq1ECh4uAgM4mpKfsQnL3XF/ZzSyCJ reidlab@rei-phone"
];
};

25
lib/attrs.nix Normal file
View file

@ -0,0 +1,25 @@
{lib, ...}: let
inherit (lib.lists) any count;
inherit (lib.attrsets) filterAttrs listToAttrs mapAttrs' mapAttrsToList;
in rec {
# attrsToList
attrsToList = attrs:
mapAttrsToList (name: value: {inherit name value;}) attrs;
# mapFilterAttrs ::
# (name -> value -> bool)
# (name -> value -> { name = any; value = any; })
# attrs
mapFilterAttrs = pred: f: attrs: filterAttrs pred (mapAttrs' f attrs);
# Generate an attribute set by mapping a function over a list of values.
genAttrs' = values: f: listToAttrs (map f values);
# anyAttrs :: (name -> value -> bool) attrs
anyAttrs = pred: attrs:
any (attr: pred attr.name attr.value) (attrsToList attrs);
# countAttrs :: (name -> value -> bool) attrs
countAttrs = pred: attrs:
count (attr: pred attr.name attr.value) (attrsToList attrs);
}

View file

@ -1,17 +1,20 @@
{ inputs, lib, pkgs, ... }:
lib.extend (self: super:
let
inherit (lib) attrValues foldr;
inherit (lib.attrsets) attrValues;
inherit (lib.fixedPoints) makeExtensible;
inherit (lib.lists) foldr;
inherit (modules) mapModules;
inherit (helpers) getSSH;
modules = import ./modules.nix { inherit lib; };
helpers = import ./helpers.nix { inherit lib; };
in {
_ = foldr (a: b: a // b) {} (attrValues (mapModules ./. (file: import file {
inherit pkgs inputs;
lib = self;
})));
}
)
modules = import ./modules.nix {
inherit lib;
self.attrs = import ./attrs.nix {
inherit lib;
self = {};
};
};
mylib =
makeExtensible (self:
mapModules ./. (file: import file {inherit self lib pkgs inputs;}));
in
mylib.extend (self: super: foldr (a: b: a // b) {} (attrValues super))

View file

@ -1,18 +0,0 @@
{ lib, ... }:
with lib;
rec {
indexFrom = origin: name: item: list: foldr
(h: t:
if h.${origin} == name && hasAttr item h
then h.${item}
else t)
(error ''
No item at the origin point ${origin} with element ${name} found.
Please make sure that the item with that origin exists, and,
failing that, that it also has the requested item defined.
'')
list;
getSSH = name: keys: indexFrom "hostname" name "ssh" keys;
}

View file

@ -1,27 +1,43 @@
{ lib, ... }:
let
inherit (builtins) attrValues readDir pathExists;
inherit (lib) id filterAttrs hasPrefix hasSuffix nameValuePair removeSuffix mapAttrs' trace fix fold isAttrs;
{
lib,
self,
...
}: let
inherit (builtins) attrValues readDir pathExists concatLists;
inherit (lib.attrsets) mapAttrsToList filterAttrs nameValuePair;
inherit (lib.strings) hasPrefix hasSuffix removeSuffix;
inherit (lib.trivial) id;
inherit (self.attrs) mapFilterAttrs;
in rec {
mapModules' = dir: fn: dirfn:
filterAttrs
(name: type: type != null && !(hasPrefix "_" name))
(mapAttrs'
(name: type:
let path = "${toString dir}/${name}"; in
if type == "directory"
then nameValuePair name (dirfn path)
else if
type == "regular" &&
name != "default.nix" &&
hasSuffix ".nix" name
then nameValuePair (removeSuffix ".nix" name) (fn path)
else nameValuePair "" null
)
(readDir dir));
mapModules = dir: fn:
mapFilterAttrs (n: v: v != null && !(hasPrefix "_" n)) (n: v: let
path = "${toString dir}/${n}";
in
if v == "directory" && pathExists "${path}/default.nix"
then nameValuePair n (fn path)
else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n
then nameValuePair (removeSuffix ".nix" n) (fn path)
else nameValuePair "" null) (readDir dir);
mapModules = dir: fn: mapModules' dir fn (path: if pathExists "${path}/default.nix" then fn path else null);
mapModulesRec = dir: fn: mapModules' dir fn (path: mapModulesRec path fn);
mapModulesRec' = dir: fn: fix (f: attrs: fold (x: xs: (if isAttrs x then f x else [x]) ++ xs) [] (attrValues attrs)) (mapModulesRec dir fn);
mapModules' = dir: fn: attrValues (mapModules dir fn);
mapModulesRec = dir: fn:
mapFilterAttrs (n: v: v != null && !(hasPrefix "_" n)) (n: v: let
path = "${toString dir}/${n}";
in
if v == "directory"
then nameValuePair n (mapModulesRec path fn)
else if v == "regular" && n != "default.nix" && hasSuffix ".nix" n
then nameValuePair (removeSuffix ".nix" n) (fn path)
else nameValuePair "" null) (readDir dir);
mapModulesRec' = dir: fn: let
dirs =
mapAttrsToList (k: _: "${dir}/${k}")
(filterAttrs (n: v: v == "directory" && !(hasPrefix "_" n))
(readDir dir));
files = attrValues (mapModules dir id);
paths = files ++ concatLists (map (d: mapModulesRec' d id) dirs);
in
map fn paths;
}

View file

@ -1,21 +1,35 @@
{ inputs, lib, pkgs, ... }:
with lib;
{
mkHost = path: attrs@{ system, ... }:
inputs,
lib,
pkgs,
self,
...
}: let
inherit (inputs.nixpkgs.lib) nixosSystem;
inherit (builtins) baseNameOf elem;
inherit (lib.attrsets) filterAttrs;
inherit (lib.modules) mkDefault;
inherit (lib.strings) removeSuffix;
inherit (self.modules) mapModules;
in rec {
mkHost = path: attrs @ {system ? "aarch64-linux", ...}:
nixosSystem {
inherit system;
specialArgs = { inherit lib inputs system; };
specialArgs = {inherit lib inputs system;};
modules = [
{
nixpkgs.pkgs = pkgs;
networking.hostName = mkDefault (removeSuffix ".nix" (baseNameOf path));
networking.hostName =
mkDefault (removeSuffix ".nix" (baseNameOf path));
}
(filterAttrs (n: v: !elem n [ "system" ]) attrs)
../.
(filterAttrs (n: v: !elem n ["system"]) attrs)
../. # /default.nix
(import path)
];
};
mapHosts = dir: attrs @ {system ? system, ...}:
mapModules dir (hostPath: mkHost hostPath attrs);
}

View file

@ -18,8 +18,5 @@ before committing, please run `nix flake check` and make sure everything is ok
## todo
- analytics using matomo
- php support in staticsites
- no more luapackagepath. please stop.
- not sure if this is cloudflare doing this or our acme config, but accessing invalid subdomains returns a dumb ssl error
- this is not related to the flake but it is to the site. transfer from namecheap to porkbun plz
- remove the lua static stuff from nginx
- add a helper for per-host architecture picking, atm it is hardcoded to `aarch64`