add nixos and darwin-nix modules
This commit is contained in:
parent
eeb8a69740
commit
ba34ac0d67
4 changed files with 129 additions and 12 deletions
|
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
Changes yet to be released are documented here.
|
Changes yet to be released are documented here.
|
||||||
|
|
||||||
|
- Add nixos, home-manager, and darwin-nix modules
|
||||||
|
|
||||||
## v0.1.0
|
## v0.1.0
|
||||||
|
|
||||||
Initial release.
|
Initial release.
|
||||||
|
|
|
||||||
28
flake.nix
28
flake.nix
|
|
@ -6,6 +6,7 @@
|
||||||
let
|
let
|
||||||
systems = [
|
systems = [
|
||||||
"x86_64-linux"
|
"x86_64-linux"
|
||||||
|
"aarch64-linux"
|
||||||
"aarch64-darwin"
|
"aarch64-darwin"
|
||||||
];
|
];
|
||||||
forAllSystems = fn: nixpkgs.lib.genAttrs systems (system: fn nixpkgs.legacyPackages.${system});
|
forAllSystems = fn: nixpkgs.lib.genAttrs systems (system: fn nixpkgs.legacyPackages.${system});
|
||||||
|
|
@ -23,7 +24,9 @@
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
checks = forAllSystems (pkgs: {
|
checks = forAllSystems (
|
||||||
|
pkgs:
|
||||||
|
{
|
||||||
tests = self.packages.${pkgs.stdenv.hostPlatform.system}.default.overrideAttrs (_: {
|
tests = self.packages.${pkgs.stdenv.hostPlatform.system}.default.overrideAttrs (_: {
|
||||||
pname = "nix-cache-login-tests";
|
pname = "nix-cache-login-tests";
|
||||||
checkPhase = ''
|
checkPhase = ''
|
||||||
|
|
@ -34,7 +37,11 @@
|
||||||
doCheck = true;
|
doCheck = true;
|
||||||
});
|
});
|
||||||
devShell = self.devShells.${pkgs.stdenv.hostPlatform.system}.default;
|
devShell = self.devShells.${pkgs.stdenv.hostPlatform.system}.default;
|
||||||
});
|
}
|
||||||
|
// pkgs.lib.optionalAttrs pkgs.stdenv.isLinux {
|
||||||
|
nixos-module = pkgs.testers.runNixOSTest (import ./nixos-test.nix self);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
devShells = forAllSystems (pkgs: {
|
devShells = forAllSystems (pkgs: {
|
||||||
default = pkgs.mkShell {
|
default = pkgs.mkShell {
|
||||||
|
|
@ -45,6 +52,23 @@
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
homeModules = {
|
||||||
|
# Workstation (Linux + macOS): home-manager module running `nix-cache-login refresh`
|
||||||
|
default =
|
||||||
|
{
|
||||||
|
config,
|
||||||
|
lib,
|
||||||
|
pkgs,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
{
|
||||||
|
imports = [ ./home-module.nix ];
|
||||||
|
services.nix-cache-login.package =
|
||||||
|
lib.mkDefault
|
||||||
|
self.packages.${pkgs.stdenv.hostPlatform.system}.default;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
nixosModules = {
|
nixosModules = {
|
||||||
# Workstation: systemd user timer+service running `nix-cache-login refresh`
|
# Workstation: systemd user timer+service running `nix-cache-login refresh`
|
||||||
default =
|
default =
|
||||||
|
|
|
||||||
62
home-module.nix
Normal file
62
home-module.nix
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
{
|
||||||
|
config,
|
||||||
|
lib,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
cfg = config.services.nix-cache-login;
|
||||||
|
in
|
||||||
|
{
|
||||||
|
options.services.nix-cache-login = {
|
||||||
|
enable = lib.mkEnableOption "nix-cache-login automatic token refresh";
|
||||||
|
package = lib.mkOption {
|
||||||
|
type = lib.types.package;
|
||||||
|
description = "The nix-cache-login package to use.";
|
||||||
|
};
|
||||||
|
refreshInterval = lib.mkOption {
|
||||||
|
type = lib.types.ints.positive;
|
||||||
|
default = 900;
|
||||||
|
description = ''
|
||||||
|
How often to attempt token refresh, in seconds.
|
||||||
|
If no valid session exists, the service logs an error and retries on
|
||||||
|
the next interval. Run {command}`nix-cache-login` to log in.
|
||||||
|
'';
|
||||||
|
example = 1800;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
config = lib.mkIf cfg.enable {
|
||||||
|
home.packages = [ cfg.package ];
|
||||||
|
systemd.user.services.nix-cache-login = {
|
||||||
|
Unit.Description = "Nix cache login - refresh access token";
|
||||||
|
Service = {
|
||||||
|
Type = "oneshot";
|
||||||
|
ExecStart = "${cfg.package}/bin/nix-cache-login refresh";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
systemd.user.timers.nix-cache-login = {
|
||||||
|
Unit.Description = "Nix cache login - periodic token refresh";
|
||||||
|
Timer = {
|
||||||
|
OnBootSec = "2min";
|
||||||
|
OnUnitActiveSec = "${toString cfg.refreshInterval}s";
|
||||||
|
};
|
||||||
|
Install.WantedBy = [ "timers.target" ];
|
||||||
|
};
|
||||||
|
|
||||||
|
launchd.agents.nix-cache-login = {
|
||||||
|
enable = true;
|
||||||
|
config = {
|
||||||
|
ProgramArguments = [
|
||||||
|
"${cfg.package}/bin/nix-cache-login"
|
||||||
|
"refresh"
|
||||||
|
];
|
||||||
|
StartInterval = cfg.refreshInterval;
|
||||||
|
RunAtLoad = true;
|
||||||
|
ProcessType = "Background";
|
||||||
|
StandardOutPath = "${config.home.homeDirectory}/Library/Logs/nix-cache-login.log";
|
||||||
|
StandardErrorPath = "${config.home.homeDirectory}/Library/Logs/nix-cache-login.log";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
29
nixos-test.nix
Normal file
29
nixos-test.nix
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
self: {
|
||||||
|
name = "nix-cache-login-nixos-module";
|
||||||
|
|
||||||
|
nodes.machine =
|
||||||
|
{ ... }:
|
||||||
|
{
|
||||||
|
imports = [ self.nixosModules.default ];
|
||||||
|
services.nix-cache-login.enable = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
testScript = ''
|
||||||
|
machine.wait_for_unit("multi-user.target")
|
||||||
|
|
||||||
|
# The module should install timer and service unit files for all users
|
||||||
|
machine.succeed("test -f /etc/systemd/user/nix-cache-login.timer")
|
||||||
|
machine.succeed("test -f /etc/systemd/user/nix-cache-login.service")
|
||||||
|
|
||||||
|
# wantedBy = ["timers.target"] should create this symlink
|
||||||
|
machine.succeed(
|
||||||
|
"test -L /etc/systemd/user/timers.target.wants/nix-cache-login.timer"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Service unit should reference the correct subcommand
|
||||||
|
unit = machine.succeed("cat /etc/systemd/user/nix-cache-login.service")
|
||||||
|
assert "nix-cache-login refresh" in unit, (
|
||||||
|
f"ExecStart not found in service unit:\n{unit}"
|
||||||
|
)
|
||||||
|
'';
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue