lol
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add support for user units

With ‘systemd.user.units’ and ‘systemd.user.services’, you can specify
units used by per-user systemd instances. For example,

systemd.user.services.foo =
{ description = "foo";
wantedBy = [ "default.target" ];
serviceConfig.ExecStart = "${pkgs.foo}/bin/foo";
};

declares a unit ‘foo.service’ that gets started automatically when the
user systemd instance starts, and is stopped when the user systemd
instance stops.

Note that there is at most one systemd instance per user: it's created
when a user logs in and there is no systemd instance for that user
yet, and it's removed when the user fully logs out (i.e. has no
sessions anymore). So if you're simultaneously logged in via X11 and a
virtual console, you get only one copy of foo.

+60 -23
+60 -23
nixos/modules/system/boot/systemd.nix
··· 24 24 ln -s /dev/null $out/${name} 25 25 ''; 26 26 27 - upstreamUnits = 27 + upstreamSystemUnits = 28 28 [ # Targets. 29 29 "basic.target" 30 30 "sysinit.target" ··· 165 165 "emergency.service" 166 166 ]; 167 167 168 - upstreamWants = 168 + upstreamSystemWants = 169 169 [ #"basic.target.wants" 170 170 "sysinit.target.wants" 171 171 "sockets.target.wants" 172 172 "local-fs.target.wants" 173 173 "multi-user.target.wants" 174 174 "timers.target.wants" 175 + ]; 176 + 177 + upstreamUserUnits = 178 + [ "basic.target" 179 + "default.target" 180 + "exit.target" 181 + "paths.target" 182 + "shutdown.target" 183 + "sockets.target" 184 + "systemd-exit.service" 185 + "timers.target" 175 186 ]; 176 187 177 188 makeJobScript = name: text: ··· 359 370 ''; 360 371 }; 361 372 362 - units = pkgs.runCommand "units" { preferLocalBuild = true; } 363 - '' 373 + generateUnits = type: units: upstreamUnits: upstreamWants: 374 + pkgs.runCommand "${type}-units" { preferLocalBuild = true; } '' 364 375 mkdir -p $out 365 376 366 377 # Copy the upstream systemd units we're interested in. 367 378 for i in ${toString upstreamUnits}; do 368 - fn=${systemd}/example/systemd/system/$i 379 + fn=${systemd}/example/systemd/${type}/$i 369 380 if ! [ -e $fn ]; then echo "missing $fn"; false; fi 370 381 if [ -L $fn ]; then 371 382 cp -pd $fn $out/ ··· 377 388 # Copy .wants links, but only those that point to units that 378 389 # we're interested in. 379 390 for i in ${toString upstreamWants}; do 380 - fn=${systemd}/example/systemd/system/$i 391 + fn=${systemd}/example/systemd/${type}/$i 381 392 if ! [ -e $fn ]; then echo "missing $fn"; false; fi 382 393 x=$out/$(basename $fn) 383 394 mkdir $x ··· 390 401 391 402 # Symlink all units provided listed in systemd.packages. 392 403 for i in ${toString cfg.packages}; do 393 - ln -s $i/etc/systemd/system/* $i/lib/systemd/system/* $out/ 404 + if [ -n "$(echo $i/etc/systemd/${type}/*)" ]; then 405 + ln -s $i/etc/systemd/${type}/* $i/lib/systemd/${type}/* $out/ 406 + fi 394 407 done 395 408 396 409 # Symlink all units defined by systemd.units. If these are also 397 410 # provided by systemd or systemd.packages, then add them as 398 411 # <unit-name>.d/overrides.conf, which makes them extend the 399 412 # upstream unit. 400 - for i in ${toString (mapAttrsToList (n: v: v.unit) cfg.units)}; do 413 + for i in ${toString (mapAttrsToList (n: v: v.unit) units)}; do 401 414 fn=$(basename $i/*) 402 415 if [ -e $out/$fn ]; then 403 416 if [ "$(readlink -f $i/$fn)" = /dev/null ]; then ··· 417 430 concatMapStrings (name2: '' 418 431 mkdir -p $out/'${name2}.wants' 419 432 ln -sfn '../${name}' $out/'${name2}.wants'/ 420 - '') unit.wantedBy) cfg.units)} 433 + '') unit.wantedBy) units)} 421 434 422 435 ${concatStrings (mapAttrsToList (name: unit: 423 436 concatMapStrings (name2: '' 424 437 mkdir -p $out/'${name2}.requires' 425 438 ln -sfn '../${name}' $out/'${name2}.requires'/ 426 - '') unit.requiredBy) cfg.units)} 439 + '') unit.requiredBy) units)} 427 440 428 - # Stupid misc. symlinks. 429 - ln -s ${cfg.defaultUnit} $out/default.target 441 + ${optionalString (type == "system") '' 442 + # Stupid misc. symlinks. 443 + ln -s ${cfg.defaultUnit} $out/default.target 430 444 431 - ln -s rescue.target $out/kbrequest.target 445 + ln -s rescue.target $out/kbrequest.target 432 446 433 - mkdir -p $out/getty.target.wants/ 434 - ln -s ../autovt@tty1.service $out/getty.target.wants/ 447 + mkdir -p $out/getty.target.wants/ 448 + ln -s ../autovt@tty1.service $out/getty.target.wants/ 435 449 436 - ln -s ../local-fs.target ../remote-fs.target ../network.target ../nss-lookup.target \ 437 - ../nss-user-lookup.target ../swap.target $out/multi-user.target.wants/ 450 + ln -s ../local-fs.target ../remote-fs.target ../network.target ../nss-lookup.target \ 451 + ../nss-user-lookup.target ../swap.target $out/multi-user.target.wants/ 452 + ''} 438 453 ''; # */ 439 454 440 455 in ··· 638 653 ''; 639 654 }; 640 655 656 + systemd.user.units = mkOption { 657 + description = "Definition of systemd per-user units."; 658 + default = {}; 659 + type = types.attrsOf types.optionSet; 660 + options = { name, config, ... }: 661 + { options = concreteUnitOptions; 662 + config = { 663 + unit = mkDefault (makeUnit name config); 664 + }; 665 + }; 666 + }; 667 + 668 + systemd.user.services = mkOption { 669 + default = {}; 670 + type = types.attrsOf types.optionSet; 671 + options = [ serviceOptions unitConfig serviceConfig ]; 672 + description = "Definition of systemd per-user service units."; 673 + }; 674 + 641 675 }; 642 676 643 677 ··· 651 685 message = "${name}: Type=oneshot services must have Restart=no"; 652 686 }) cfg.services; 653 687 654 - system.build.units = units; 688 + system.build.units = cfg.units; 655 689 656 690 environment.systemPackages = [ systemd ]; 657 691 658 - environment.etc."systemd/system".source = units; 692 + environment.etc."systemd/system".source = 693 + generateUnits "system" cfg.units upstreamSystemUnits upstreamSystemWants; 694 + 695 + environment.etc."systemd/user".source = 696 + generateUnits "user" cfg.user.units upstreamUserUnits []; 659 697 660 698 environment.etc."systemd/system.conf".text = 661 699 '' ··· 719 757 (v: let n = escapeSystemdPath v.where; 720 758 in nameValuePair "${n}.automount" (automountToUnit n v)) cfg.automounts); 721 759 760 + systemd.user.units = 761 + mapAttrs' (n: v: nameValuePair "${n}.service" (serviceToUnit n v)) cfg.user.services; 762 + 722 763 system.requiredKernelConfig = map config.lib.kernelConfig.isEnabled [ 723 764 "CGROUPS" "AUTOFS4_FS" "DEVTMPFS" 724 765 ]; ··· 752 793 # in systemd to provide XDG_RUNTIME_DIR. 753 794 startSession = true; 754 795 }; 755 - 756 - # Provide systemd user units. FIXME: Should make this definable, 757 - # just like the system units. 758 - environment.etc."systemd/user".source = "${systemd}/example/systemd/user"; 759 796 760 797 environment.etc."tmpfiles.d/x11.conf".source = "${systemd}/example/tmpfiles.d/x11.conf"; 761 798