Add initial tmproot module

This commit is contained in:
Jack O'Sullivan 2022-02-09 20:41:51 +00:00
parent fd0bbb2961
commit ee353607d2
3 changed files with 80 additions and 11 deletions

View File

@ -60,6 +60,7 @@
imports = [ (import path') ]; imports = [ (import path') ];
}) { }) {
common = "common.nix"; common = "common.nix";
tmproot = "tmproot.nix";
server = "server.nix"; server = "server.nix";
}; };

View File

@ -12,19 +12,22 @@
defaultUsername = "dev"; defaultUsername = "dev";
uname = config.my.user.name; uname = config.my.user.name;
in { in {
my.user = rec { my = {
user = {
name = mkDefault defaultUsername; name = mkDefault defaultUsername;
isNormalUser = true; isNormalUser = true;
uid = mkDefault 1000; uid = mkDefault 1000;
extraGroups = mkDefault [ "wheel" ]; extraGroups = mkDefault [ "wheel" ];
password = mkDefault "hunter2"; # TODO: secrets... password = mkDefault "hunter2"; # TODO: secrets...
}; };
};
time.timeZone = mkDefault "Europe/Dublin"; time.timeZone = mkDefault "Europe/Dublin";
users.mutableUsers = false; users = {
users.users.${uname} = mkAliasDefinitions options.my.user; mutableUsers = false;
users.groups.${uname}.gid = mkDefault config.users.users.${uname}.uid; users.${uname} = mkAliasDefinitions options.my.user;
};
security = { security = {
sudo.enable = mkDefault false; sudo.enable = mkDefault false;
@ -43,11 +46,15 @@
}; };
environment.systemPackages = with pkgs; [ environment.systemPackages = with pkgs; [
bash-completion
vim vim
htop
iperf3 iperf3
]; ];
system.stateVersion = "21.11"; system = {
system.configurationRevision = with inputs; mkIf (self ? rev) self.rev; stateVersion = "21.11";
configurationRevision = with inputs; mkIf (self ? rev) self.rev;
};
}; };
} }

61
modules/tmproot.nix Normal file
View File

@ -0,0 +1,61 @@
{ lib, pkgs, inputs, config, ... }:
let
inherit (lib) concatStringsSep mkIf mkDefault mkAliasDefinitions;
inherit (lib.my) mkOpt mkBoolOpt;
cfg = config.my.tmproot;
showUnsaved =
''
#!${pkgs.python310}/bin/python
import stat
import sys
import os
ignored = [
${concatStringsSep ",\n " (map (p: "'${p}'") cfg.ignoreUnsaved)}
]
base = '/'
base_dev = os.stat(base).st_dev
def recurse(p, link=None):
try:
for ignore in ignored:
if p.startswith(ignore):
return
st = os.lstat(p)
if st.st_dev != base_dev:
return
if stat.S_ISLNK(st.st_mode):
target = os.path.realpath(p, strict=False)
if os.access(target, os.F_OK):
recurse(target, link=p)
return
elif stat.S_ISDIR(st.st_mode):
for e in os.listdir(p):
recurse(os.path.join(p, e))
return
print(link or p)
except PermissionError as ex:
print(f'{p}: {ex.strerror}', file=sys.stderr)
recurse(base)
'';
in {
options.my.tmproot = with lib.types; {
enable = mkBoolOpt true;
ignoreUnsaved = mkOpt (listOf str) [
"/tmp"
];
};
config = mkIf cfg.enable {
environment.systemPackages = [
(pkgs.writeScriptBin "tmproot-unsaved" showUnsaved)
];
};
}