fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3let 4 5cfg = config . systemd . user ; 6 7inherit ( lib ) getAttr hm isBool literalExpression mkIf mkMerge mkOption types ; 8 9# From <nixpkgs/nixos/modules/system/boot/systemd-lib.nix> 10mkPathSafeName = 11lib . replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ]; 12 13toSystemdIni = lib . generators . toINI { 14listsAsDuplicateKeys = true ; 15mkKeyValue = key :value : 16let 17value' = if isBool value then 18( if value then "true" else "false" ) 19else 20toString value ; 21in " ${ key } = ${ value' } " ; 22}; 23 24buildService = style :name :serviceCfg : 25let 26filename = " ${ name } . ${ style } " ; 27pathSafeName = mkPathSafeName filename ; 28 29# Needed because systemd derives unit names from the ultimate 30# link target. 31source = pkgs . writeTextFile { 32name = pathSafeName ; 33text = toSystemdIni serviceCfg ; 34destination = "/ ${ filename } " ; 35} + "/ ${ filename } " ; 36 37install = variant :target :{ 38name = "systemd/user/ ${ target } . ${ variant } / ${ filename } " ; 39value = { inherit source ; }; 40}; 41in lib . singleton { 42name = "systemd/user/ ${ filename } " ; 43value = { inherit source ; }; 44} ++ map ( install "wants" ) ( serviceCfg . Install . WantedBy or [ ]) 45++ map ( install "requires" ) ( serviceCfg . Install . RequiredBy or [ ]); 46 47buildServices = style :serviceCfgs : 48lib . concatLists ( lib . mapAttrsToList ( buildService style ) serviceCfgs ); 49 50servicesStartTimeoutMs = builtins . toString cfg . servicesStartTimeoutMs ; 51 52unitType = unitKind : 53with types ; 54let primitive = oneOf [ bool int str path ]; 55in attrsOf ( attrsOf ( attrsOf ( either primitive ( listOf primitive )))) // { 56description = "systemd ${ unitKind } unit configuration" ; 57}; 58 59unitDescription = type :'' 60Definition of systemd per-user ${ type } units. Attributes are 61merged recursively. 62 63Note that the attributes follow the capitalization and naming used 64by systemd. More details can be found in 65{manpage}`systemd. ${ type } (5)`. 66'' ; 67 68unitExample = type : 69literalExpression '' 70{ 71${ lib . toLower type } -name = { 72Unit = { 73Description = "Example description"; 74Documentation = [ "man:example(1)" "man:example(5)" ]; 75}; 76 77${ type } = { 78… 79}; 80}; 81}; 82'' ; 83 84sessionVariables = mkIf ( cfg . sessionVariables != { }) { 85"environment.d/10-home-manager.conf" . text = lib . concatStringsSep "\n" 86( lib . mapAttrsToList ( n :v :" ${ n } = ${ toString v } " ) cfg . sessionVariables ) 87+ "\n" ; 88}; 89 90in { 91meta . maintainers = [ lib . maintainers . rycee ]; 92 93options = { 94systemd . user = { 95systemctlPath = mkOption { 96default = " ${ pkgs . systemd } /bin/systemctl" ; 97defaultText = literalExpression ''"''${pkgs.systemd}/bin/systemctl"'' ; 98type = types . str ; 99description = '' 100Absolute path to the {command}`systemctl` tool. This 101option may need to be set if running Home Manager on a 102non-NixOS distribution. 103'' ; 104}; 105 106services = mkOption { 107default = { }; 108type = unitType "service" ; 109description = ( unitDescription "service" ); 110example = unitExample "Service" ; 111}; 112 113slices = mkOption { 114default = { }; 115type = unitType "slice" ; 116description = ( unitDescription "slice" ); 117example = unitExample "Slice" ; 118}; 119 120sockets = mkOption { 121default = { }; 122type = unitType "socket" ; 123description = ( unitDescription "socket" ); 124example = unitExample "Socket" ; 125}; 126 127targets = mkOption { 128default = { }; 129type = unitType "target" ; 130description = ( unitDescription "target" ); 131example = unitExample "Target" ; 132}; 133 134timers = mkOption { 135default = { }; 136type = unitType "timer" ; 137description = ( unitDescription "timer" ); 138example = unitExample "Timer" ; 139}; 140 141paths = mkOption { 142default = { }; 143type = unitType "path" ; 144description = ( unitDescription "path" ); 145example = unitExample "Path" ; 146}; 147 148mounts = mkOption { 149default = { }; 150type = unitType "mount" ; 151description = ( unitDescription "mount" ); 152example = unitExample "Mount" ; 153}; 154 155automounts = mkOption { 156default = { }; 157type = unitType "automount" ; 158description = ( unitDescription "automount" ); 159example = unitExample "Automount" ; 160}; 161 162startServices = mkOption { 163default = "suggest" ; 164type = with types ; 165either bool ( enum [ "suggest" "legacy" "sd-switch" ]); 166apply = p :if isBool p then if p then "legacy" else "suggest" else p ; 167description = '' 168Whether new or changed services that are wanted by active targets 169should be started. Additionally, stop obsolete services from the 170previous generation. 171 172The alternatives are 173 174`suggest` (or `false`) 175: Use a very simple shell script to print suggested 176{command}`systemctl` commands to run. You will have to 177manually run those commands after the switch. 178 179`legacy` (or `true`) 180: Use a Ruby script to, in a more robust fashion, determine the 181necessary changes and automatically run the 182{command}`systemctl` commands. 183 184`sd-switch` 185: Use sd-switch, a third party application, to perform the service 186updates. This tool offers more features while having a small 187closure size. Note, it requires a fully functional user D-Bus 188session. Once tested and deemed sufficiently robust, this will 189become the default. 190'' ; 191}; 192 193servicesStartTimeoutMs = mkOption { 194default = 0 ; 195type = types . ints . unsigned ; 196description = '' 197How long to wait for started services to fail until their start is 198considered successful. The value 0 indicates no timeout. 199'' ; 200}; 201 202sessionVariables = mkOption { 203default = { }; 204type = with types ; attrsOf ( either int str ); 205example = { EDITOR = "vim" ; }; 206description = '' 207Environment variables that will be set for the user session. 208The variable values must be as described in 209{manpage}`environment.d(5)`. 210'' ; 211}; 212}; 213}; 214 215# If we run under a Linux system we assume that systemd is 216# available, in particular we assume that systemctl is in PATH. 217# Do not install any user services if username is root. 218config = mkIf ( pkgs . stdenv . isLinux && config . home . username != "root" ) { 219xdg . configFile = mkMerge [ 220( lib . listToAttrs (( buildServices "service" cfg . services ) 221++ ( buildServices "slice" cfg . slices ) 222++ ( buildServices "socket" cfg . sockets ) 223++ ( buildServices "target" cfg . targets ) 224++ ( buildServices "timer" cfg . timers ) 225++ ( buildServices "path" cfg . paths ) 226++ ( buildServices "mount" cfg . mounts ) 227++ ( buildServices "automount" cfg . automounts ))) 228 229sessionVariables 230]; 231 232# Run systemd service reload if user is logged in. If we're 233# running this from the NixOS module then XDG_RUNTIME_DIR is not 234# set and systemd commands will fail. We'll therefore have to 235# set it ourselves in that case. 236home . activation . reloadSystemd = hm . dag . entryAfter [ "linkGeneration" ] ( let 237cmd = { 238suggest = '' 239PATH= ${ dirOf cfg . systemctlPath } :$PATH \ 240bash ${ ./systemd-activate.sh } "''${oldGenPath=}" "$newGenPath" 241'' ; 242legacy = '' 243PATH= ${ dirOf cfg . systemctlPath } :$PATH \ 244${ pkgs . ruby } /bin/ruby ${ ./systemd-activate.rb } \ 245"''${oldGenPath=}" "$newGenPath" " ${ servicesStartTimeoutMs } " 246'' ; 247sd-switch = let 248timeoutArg = if cfg . servicesStartTimeoutMs != 0 then 249"--timeout " + servicesStartTimeoutMs 250else 251"" ; 252in '' 253${ pkgs . sd-switch } /bin/sd-switch \ 254''${DRY_RUN:+--dry-run} $VERBOSE_ARG ${ timeoutArg } \ 255''${oldGenPath:+--old-units $oldGenPath/home-files/.config/systemd/user} \ 256--new-units $newGenPath/home-files/.config/systemd/user 257'' ; 258}; 259 260ensureRuntimeDir = 261"XDG_RUNTIME_DIR=\${XDG_RUNTIME_DIR:-/run/user/$(id -u)}" ; 262 263systemctl = " ${ ensureRuntimeDir } ${ cfg . systemctlPath } " ; 264in '' 265systemdStatus=$( ${ systemctl } --user is-system-running 2>&1 || true) 266 267if [[ $systemdStatus == 'running' || $systemdStatus == 'degraded' ]]; then 268if [[ $systemdStatus == 'degraded' ]]; then 269warnEcho "The user systemd session is degraded:" 270${ systemctl } --user --no-pager --state=failed 271warnEcho "Attempting to reload services anyway..." 272fi 273 274${ ensureRuntimeDir } \ 275${ getAttr cfg . startServices cmd } 276else 277echo "User systemd daemon not running. Skipping reload." 278fi 279 280unset systemdStatus 281'' ); 282}; 283}