Skip to content

Commit

Permalink
treewide: nixpkgs-fmt -> nixfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
nikstur committed Aug 6, 2024
1 parent 72e8b3f commit a310393
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 234 deletions.
19 changes: 9 additions & 10 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
(import
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }
).defaultNix
(import (
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
) { src = ./.; }).defaultNix
166 changes: 95 additions & 71 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,80 +26,104 @@

};

outputs = inputs@{ self, flake-parts, systems, ... }: flake-parts.lib.mkFlake { inherit inputs; } ({ moduleWithSystem, ... }: {
systems = import systems;

imports = [
inputs.pre-commit-hooks-nix.flakeModule
];

flake.nixosModules.userborn = moduleWithSystem (
perSystem@{ config }:
{ ... }: {
imports = [
./nix/modules/userborn.nix
];

services.userborn.package = perSystem.config.packages.userborn;
}
);

perSystem = { config, system, pkgs, lib, ... }:
outputs =
inputs@{
self,
flake-parts,
systems,
...
}:
flake-parts.lib.mkFlake { inherit inputs; } (
{ moduleWithSystem, ... }:
{
packages = import ./nix/packages { inherit pkgs; } // {
default = config.packages.userborn;
};

checks = {
clippy = config.packages.userborn.overrideAttrs (_: previousAttrs: {
pname = previousAttrs.pname + "-clippy";
nativeCheckInputs = (previousAttrs.nativeCheckInputs or [ ]) ++ [ pkgs.clippy ];
checkPhase = "cargo clippy";
});
rustfmt = config.packages.userborn.overrideAttrs (_: previousAttrs: {
pname = previousAttrs.pname + "-rustfmt";
nativeCheckInputs = (previousAttrs.nativeCheckInputs or [ ]) ++ [ pkgs.rustfmt ];
checkPhase = "cargo fmt --check";
});
} // (import ./nix/tests {
inherit pkgs;
extraBaseModules = {
inherit (self.nixosModules) userborn;
};
});

pre-commit = {
check.enable = true;

settings = {
hooks = {
nixpkgs-fmt.enable = true;
statix.enable = true;
systems = import systems;

imports = [ inputs.pre-commit-hooks-nix.flakeModule ];

flake.nixosModules.userborn = moduleWithSystem (
perSystem@{ config }:
{ ... }:
{
imports = [ ./nix/modules/userborn.nix ];

services.userborn.package = perSystem.config.packages.userborn;
}
);

perSystem =
{
config,
system,
pkgs,
lib,
...
}:
{
packages = import ./nix/packages { inherit pkgs; } // {
default = config.packages.userborn;
};
};
};

devShells.default = pkgs.mkShell {
shellHook = ''
${config.pre-commit.installationScript}
'';

packages = [
pkgs.niv
pkgs.clippy
pkgs.rustfmt
pkgs.cargo-machete
pkgs.cargo-edit
pkgs.cargo-bloat
pkgs.cargo-deny
pkgs.cargo-cyclonedx
];

inputsFrom = [ config.packages.userborn ];
checks =
{
clippy = config.packages.userborn.overrideAttrs (
_: previousAttrs: {
pname = previousAttrs.pname + "-clippy";
nativeCheckInputs = (previousAttrs.nativeCheckInputs or [ ]) ++ [ pkgs.clippy ];
checkPhase = "cargo clippy";
}
);
rustfmt = config.packages.userborn.overrideAttrs (
_: previousAttrs: {
pname = previousAttrs.pname + "-rustfmt";
nativeCheckInputs = (previousAttrs.nativeCheckInputs or [ ]) ++ [ pkgs.rustfmt ];
checkPhase = "cargo fmt --check";
}
);
}
// (import ./nix/tests {
inherit pkgs;
extraBaseModules = {
inherit (self.nixosModules) userborn;
};
});

pre-commit = {
check.enable = true;

settings = {
hooks = {
nixfmt = {
enable = true;
package = pkgs.nixfmt-rfc-style;
};
statix.enable = true;
};
};
};

RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
};
devShells.default = pkgs.mkShell {
shellHook = ''
${config.pre-commit.installationScript}
'';

packages = [
pkgs.niv
pkgs.nixfmt-rfc-style
pkgs.clippy
pkgs.rustfmt
pkgs.cargo-machete
pkgs.cargo-edit
pkgs.cargo-bloat
pkgs.cargo-deny
pkgs.cargo-cyclonedx
];

inputsFrom = [ config.packages.userborn ];

RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
};

};
});
};
}
);
}
129 changes: 74 additions & 55 deletions nix/modules/userborn.nix
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
{ utils, config, lib, pkgs, ... }:
{
utils,
config,
lib,
pkgs,
...
}:

let

cfg = config.services.userborn;
userCfg = config.users;

userbornConfig = {
groups = lib.mapAttrsToList
(username: opts: {
inherit (opts)
name
gid
members
;
})
config.users.groups;

users = lib.mapAttrsToList
(username: opts: {
inherit (opts)
name
uid
group
description
home
password
hashedPassword
hashedPasswordFile
initialPassword
initialHashedPassword
;
isNormal = opts.isNormalUser;
shell = utils.toShellPath opts.shell;
})
config.users.users;
groups = lib.mapAttrsToList (username: opts: {
inherit (opts) name gid members;
}) config.users.groups;

users = lib.mapAttrsToList (username: opts: {
inherit (opts)
name
uid
group
description
home
password
hashedPassword
hashedPasswordFile
initialPassword
initialHashedPassword
;
isNormal = opts.isNormalUser;
shell = utils.toShellPath opts.shell;
}) config.users.users;

};

Expand All @@ -42,7 +40,11 @@ let
immutableEtc = config.system.etc.overlay.enable && !config.system.etc.overlay.mutable;
passwordFilesLocation = if immutableEtc then cfg.immutablePasswordFilesLocation else "/etc";
# The filenames created by userborn.
passwordFiles = [ "group" "passwd" "shadow" ];
passwordFiles = [
"group"
"passwd"
"shadow"
];

in
{
Expand Down Expand Up @@ -82,23 +84,35 @@ in

# Create home directories, do not create /var/empty even if that's a user's
# home.
tmpfiles.settings.home-directories = lib.mapAttrs'
(username: opts: lib.nameValuePair opts.home {
tmpfiles.settings.home-directories = lib.mapAttrs' (
username: opts:
lib.nameValuePair opts.home {
d = {
mode = opts.homeMode;
user = username;
inherit (opts) group;
};
})
(lib.filterAttrs (_username: opts: opts.home != "/var/empty") userCfg.users);
}
) (lib.filterAttrs (_username: opts: opts.home != "/var/empty") userCfg.users);

services.userborn = {
wantedBy = [ "sysinit.target" ];
requiredBy = [ "sysinit-reactivation.target" ];
after = [ "systemd-remount-fs.service" "systemd-tmpfiles-setup-dev-early.service" ];
before = [ "systemd-tmpfiles-setup-dev.service" "sysinit.target" "shutdown.target" "sysinit-reactivation.target" ];
after = [
"systemd-remount-fs.service"
"systemd-tmpfiles-setup-dev-early.service"
];
before = [
"systemd-tmpfiles-setup-dev.service"
"sysinit.target"
"shutdown.target"
"sysinit-reactivation.target"
];
conflicts = [ "shutdown.target" ];
restartTriggers = [ userbornConfigJson passwordFilesLocation ];
restartTriggers = [
userbornConfigJson
passwordFilesLocation
];
# This way we don't have to re-declare all the dependencies to other
# services again.
aliases = [ "systemd-sysusers.service" ];
Expand All @@ -119,36 +133,41 @@ in
ExecStart = "${cfg.package}/bin/userborn ${userbornConfigJson} ${passwordFilesLocation}";

ExecStartPre = lib.mkMerge [
(lib.mkIf (!config.system.etc.overlay.mutable)
[ "${pkgs.coreutils}/bin/mkdir -p ${passwordFilesLocation}" ]
)
(lib.mkIf (!config.system.etc.overlay.mutable) [
"${pkgs.coreutils}/bin/mkdir -p ${passwordFilesLocation}"
])

# Make the source files writable before executing userborn.
(lib.mkIf (!userCfg.mutableUsers)
(lib.map
(file: "-${pkgs.util-linux}/bin/umount ${passwordFilesLocation}/${file}")
passwordFiles)
)
(lib.mkIf (!userCfg.mutableUsers) (
lib.map (file: "-${pkgs.util-linux}/bin/umount ${passwordFilesLocation}/${file}") passwordFiles
))
];

# Make the source files read-only after userborn has finished.
ExecStartPost = lib.mkIf (!userCfg.mutableUsers)
(lib.map
(file: "${pkgs.util-linux}/bin/mount --bind -o ro ${passwordFilesLocation}/${file} ${passwordFilesLocation}/${file}")
passwordFiles);
ExecStartPost = lib.mkIf (!userCfg.mutableUsers) (
lib.map (
file:
"${pkgs.util-linux}/bin/mount --bind -o ro ${passwordFilesLocation}/${file} ${passwordFilesLocation}/${file}"
) passwordFiles
);
};
};
};

# Statically create the symlinks to immutablePasswordFilesLocation when
# using an immutable /etc because we will not be able to do it at
# runtime!
environment.etc = lib.mkIf immutableEtc (lib.listToAttrs (lib.map
(file: lib.nameValuePair file {
source = "${cfg.immutablePasswordFilesLocation}/${file}";
mode = "direct-symlink";
})
passwordFiles));
environment.etc = lib.mkIf immutableEtc (
lib.listToAttrs (
lib.map (
file:
lib.nameValuePair file {
source = "${cfg.immutablePasswordFilesLocation}/${file}";
mode = "direct-symlink";
}
) passwordFiles
)
);
};

meta.maintainers = with lib.maintainers; [ nikstur ];
Expand Down
Loading

0 comments on commit a310393

Please sign in to comment.