Skip to content

Commit

Permalink
config(pre-commit/nix): Switch to nixfmt-rfc-style for *.nix files
Browse files Browse the repository at this point in the history
Nix RFC 166 [1] has been officially accepted and `nixfmt-rfc-style` [2] is the
official formatter implementation conforming to the rules agreed upon in the RFC.

This commit configures the the Devenv `pre-commit` [3] options to replace
our previous formatter `alejandra` [4] with `nixfmt-rfc-style` [2].
  • Loading branch information
MartinNikov committed Dec 20, 2024
1 parent 30b16fa commit 68e732c
Show file tree
Hide file tree
Showing 31 changed files with 1,414 additions and 1,210 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
94 changes: 56 additions & 38 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -209,49 +209,67 @@
};
};

outputs = inputs @ {
self,
nixpkgs,
flake-parts,
...
}: let
lib = import "${nixpkgs}/lib";
flake = import "${self}/flake.nix";
in
flake-parts.lib.mkFlake {inherit inputs;} {
outputs =
inputs@{
self,
nixpkgs,
flake-parts,
...
}:
let
lib = import "${nixpkgs}/lib";
flake = import "${self}/flake.nix";
in
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
./modules
./packages
];
systems = ["x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin"];
perSystem = {
system,
pkgs,
inputs',
...
}: {
_module.args.pkgs = import nixpkgs {
inherit system;
config.allowUnfree = true;
};
devShells.default = import ./shells/default.nix {inherit pkgs flake inputs';};
devShells.ci = import ./shells/ci.nix {inherit pkgs;};
};
flake.lib.create = {
rootDir,
machinesDir ? null,
usersDir ? null,
}: {
dirs = {
lib = self + "/lib";
services = self + "/services";
modules = self + "/modules";
machines = rootDir + "/machines";
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
perSystem =
{
system,
pkgs,
inputs',
...
}:
{
_module.args.pkgs = import nixpkgs {
inherit system;
config.allowUnfree = true;
};
devShells.default = import ./shells/default.nix { inherit pkgs flake inputs'; };
devShells.ci = import ./shells/ci.nix { inherit pkgs; };
};
libs = {
make-config = import ./lib/make-config.nix {inherit lib rootDir machinesDir usersDir;};
utils = import ./lib {inherit usersDir rootDir machinesDir;};
flake.lib.create =
{
rootDir,
machinesDir ? null,
usersDir ? null,
}:
{
dirs = {
lib = self + "/lib";
services = self + "/services";
modules = self + "/modules";
machines = rootDir + "/machines";
};
libs = {
make-config = import ./lib/make-config.nix {
inherit
lib
rootDir
machinesDir
usersDir
;
};
utils = import ./lib { inherit usersDir rootDir machinesDir; };
};
};
};
};
}
12 changes: 9 additions & 3 deletions lib/current-flake.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
let
currentFlake = builtins.fromJSON (builtins.readFile ../flake.lock);
inherit (currentFlake.nodes.nixos-2311.locked) owner repo rev narHash;
inherit (currentFlake.nodes.nixos-2311.locked)
owner
repo
rev
narHash
;
nixpkgs = builtins.fetchTarball {
url = "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz";
sha256 = narHash;
};
flake = import ../flake.nix;
in {
in
{
inherit currentFlake flake;
lib = (import nixpkgs {system = "x86_64-linux";}).lib;
lib = (import nixpkgs { system = "x86_64-linux"; }).lib;
}
99 changes: 54 additions & 45 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,90 @@
rootDir,
machinesDir,
...
}: let
}:
let
inherit (import ./current-flake.nix) lib;
inherit (builtins) attrValues map filter concatLists length listToAttrs typeOf split tail;
inherit (builtins)
attrValues
map
filter
concatLists
length
listToAttrs
typeOf
split
tail
;
inherit (lib) pipe filterAttrs strings;
inherit (strings) concatStringsSep;
in rec {
isSubsetOf = needle: haystack:
length (lib.lists.intersectLists needle haystack)
== length needle;
in
rec {
isSubsetOf = needle: haystack: length (lib.lists.intersectLists needle haystack) == length needle;

haveCommonElements = needle: haystack:
length (lib.lists.intersectLists needle haystack)
> 0;
haveCommonElements = needle: haystack: length (lib.lists.intersectLists needle haystack) > 0;

allUsersMembersOfAllGroups = groups: allUsersMembersOfAllGroups' usersInfo groups;
allUsersMembersOfAllGroups' = users: groups:
if groups == []
then {}
allUsersMembersOfAllGroups' =
users: groups:
if groups == [ ] then
{ }
else
filterAttrs
(key: value: isSubsetOf groups (value.extraGroups or []))
users;
filterAttrs (key: value: isSubsetOf groups (value.extraGroups or [ ])) users;

allUsersMembersOfAnyGroup = groups: allUsersMembersOfAnyGroup' usersInfo groups;
allUsersMembersOfAnyGroup' = users: groups:
if groups == []
then {}
allUsersMembersOfAnyGroup' =
users: groups:
if groups == [ ] then
{ }
else
filterAttrs
(key: value: haveCommonElements groups (value.extraGroups or []))
users;
filterAttrs (key: value: haveCommonElements groups (value.extraGroups or [ ])) users;

missing = attrs: key:
!(attrs ? "${key}");
missing = attrs: key: !(attrs ? "${key}");

allAssignedGroups = predefinedGroups: allAssignedGroups' usersInfo predefinedGroups;
allAssignedGroups' = users: predefinedGroups:
allAssignedGroups' =
users: predefinedGroups:
pipe users [
attrValues
(map (u: u.extraGroups or []))
(map (u: u.extraGroups or [ ]))
concatLists
lib.lists.unique
(filter (g: missing predefinedGroups g))
(map (g: {
name = g;
value = {};
value = { };
}))
listToAttrs
];

allUserKeysForGroup = groups: allUserKeysForGroup' usersInfo groups;
allUserKeysForGroup' = users: groups:
concatLists
(map (value: value.openssh.authorizedKeys.keys or [])
(attrValues (allUsersMembersOfAnyGroup' users groups)));
allUserKeysForGroup' =
users: groups:
concatLists (
map (value: value.openssh.authorizedKeys.keys or [ ]) (
attrValues (allUsersMembersOfAnyGroup' users groups)
)
);

zfsFileSystems = datasetList: let
zfsRoot = "zfs_root";
splitPath = path: filter (x: (typeOf x) == "string") (split "/" path);
pathTail = path: concatStringsSep "/" (tail (splitPath path));
makeZfs = zfsDataset: {
name = "/" + pathTail zfsDataset;
value = {
device = "${zfsRoot}/${zfsDataset}";
fsType = "zfs";
options = ["zfsutil"];
zfsFileSystems =
datasetList:
let
zfsRoot = "zfs_root";
splitPath = path: filter (x: (typeOf x) == "string") (split "/" path);
pathTail = path: concatStringsSep "/" (tail (splitPath path));
makeZfs = zfsDataset: {
name = "/" + pathTail zfsDataset;
value = {
device = "${zfsRoot}/${zfsDataset}";
fsType = "zfs";
options = [ "zfsutil" ];
};
};
};
in
in
listToAttrs (map makeZfs datasetList);

allUsers = builtins.attrNames (
lib.filterAttrs
(n: v: v == "directory")
(builtins.readDir "${usersDir}")
lib.filterAttrs (n: v: v == "directory") (builtins.readDir "${usersDir}")
);

readUserInfo = user: import "${usersDir}/${user}/user-info.nix";
Expand Down
9 changes: 6 additions & 3 deletions lib/import-agenix.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
moduleName: {
moduleName:
{
config,
lib,
dirs,
...
}: let
}:
let
machineConfigPath = config.mcl.host-info.configPath;
secretDir = "${machineConfigPath}/secrets/${moduleName}";
vmSecretDir = "${vmConfig}/secrets/${moduleName}";
secrets = import "${dirs.services}/${moduleName}/agenix.nix";
in {
in
{
age.secrets = secrets secretDir;

virtualisation.vmVariant = {
Expand Down
21 changes: 13 additions & 8 deletions lib/make-config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
machinesDir,
usersDir,
...
}: rec {
getMachines = type: (lib.pipe (builtins.readDir "${machinesDir}/${type}") [
(lib.filterAttrs (n: v: v == "directory" && !(lib.hasPrefix "_" n)))
builtins.attrNames
]);
}:
rec {
getMachines =
type:
(lib.pipe (builtins.readDir "${machinesDir}/${type}") [
(lib.filterAttrs (n: v: v == "directory" && !(lib.hasPrefix "_" n)))
builtins.attrNames
]);

allServers = getMachines "server";
allDesktops = getMachines "desktop";
allMachines = allServers ++ allDesktops;

nixosConfigurations = machines: configurations:
nixosConfigurations =
machines: configurations:
(lib.genAttrs machines (configurations false))
// (lib.mapAttrs' (name: value: lib.nameValuePair "${name}-vm" value)
(lib.genAttrs machines (configurations true)));
// (lib.mapAttrs' (name: value: lib.nameValuePair "${name}-vm" value) (
lib.genAttrs machines (configurations true)
));
}
32 changes: 18 additions & 14 deletions lib/shard-attrs.nix
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
lib: {
shardAttrs = attrs: shardSize: let
attrNames = builtins.attrNames attrs;
shardCount = builtins.ceil ((0.0 + (builtins.length attrNames)) / shardSize);
attrNameShards = lib.pipe (lib.range 0 (shardCount - 1)) [
(builtins.map (i: lib.sublist (i * shardSize) shardSize attrNames))
];
shards = lib.pipe attrNameShards [
(lib.imap0 (i: shard: {
name = builtins.toString i;
value = lib.genAttrs shard (key: attrs.${key});
}))
lib.listToAttrs
];
in
shardAttrs =
attrs: shardSize:
let
attrNames = builtins.attrNames attrs;
shardCount = builtins.ceil ((0.0 + (builtins.length attrNames)) / shardSize);
attrNameShards = lib.pipe (lib.range 0 (shardCount - 1)) [
(builtins.map (i: lib.sublist (i * shardSize) shardSize attrNames))
];
shards = lib.pipe attrNameShards [
(lib.imap0 (
i: shard: {
name = builtins.toString i;
value = lib.genAttrs shard (key: attrs.${key});
}
))
lib.listToAttrs
];
in
shards;
}
Loading

0 comments on commit 68e732c

Please sign in to comment.