fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6inherit ( pkgs . stdenv . hostPlatform ) isDarwin ; 7inherit ( lib . generators ) toPlist ; 8 9cfg = config . launchd ; 10labelPrefix = "org.nix-community.home." ; 11dstDir = " ${ config . home . homeDirectory } /Library/LaunchAgents" ; 12 13launchdConfig = { config , name , ...} :{ 14options = { 15enable = mkEnableOption name ; 16config = mkOption { 17type = types . submodule ( import ./launchd.nix ); 18default = { }; 19example = literalExpression '' 20{ 21ProgramArguments = [ "/usr/bin/say" "Good afternoon" ]; 22StartCalendarInterval = [ 23{ 24Hour = 12; 25Minute = 0; 26} 27]; 28} 29'' ; 30description = '' 31Define a launchd job. See {manpage}`launchd.plist(5)` for details. 32'' ; 33}; 34}; 35 36config = { config . Label = mkDefault " ${ labelPrefix }${ name } " ; }; 37}; 38 39toAgent = config :pkgs . writeText " ${ config . Label } .plist" ( toPlist { } config ); 40 41agentPlists = 42mapAttrs' ( n :v :nameValuePair " ${ v . config . Label } .plist" ( toAgent v . config )) 43( filterAttrs ( n :v :v . enable ) cfg . agents ); 44 45agentsDrv = pkgs . runCommand "home-manager-agents" { } '' 46mkdir -p " $ out " 47 48declare -A plists 49plists =( ${ 50concatStringsSep " " 51( mapAttrsToList ( name : value : "[' ${ name } ']=' ${ value } '" ) agentPlists ) 52} ) 53 54for dest in "'' $ { !plists[@]} "; do 55src="'' $ { plists [ $ dest ]}" 56ln -s " $ src " " $ out/ $ dest " 57done 58'' ; 59in { 60meta . maintainers = with maintainers ; [ midchildan ]; 61 62options . launchd = { 63enable = mkOption { 64type = types . bool ; 65default = isDarwin ; 66defaultText = literalExpression "pkgs.stdenv.hostPlatform.isDarwin" ; 67description = '' 68Whether to enable Home Manager to define per-user daemons by making use 69of launchd's LaunchAgents. 70'' ; 71}; 72 73agents = mkOption { 74type = with types ; attrsOf ( submodule launchdConfig ); 75default = { }; 76description = "Define LaunchAgents." ; 77}; 78}; 79 80config = mkMerge [ 81{ 82assertions = [{ 83assertion = ( cfg . enable && agentPlists != { }) -> isDarwin ; 84message = let names = lib . concatStringsSep ", " ( attrNames agentPlists ); 85in "Must use Darwin for modules that require Launchd: " + names ; 86}]; 87} 88 89( mkIf isDarwin { 90home . extraBuilderCommands = '' 91ln -s " ${ agentsDrv } " $out/LaunchAgents 92'' ; 93 94home . activation . checkLaunchAgents = 95hm . dag . entryBefore [ "writeBoundary" ] '' 96checkLaunchAgents() { 97local oldDir newDir dstDir err 98oldDir="" 99err=0 100if [[ -n "''${oldGenPath:-}" ]]; then 101oldDir="$(readlink -m "$oldGenPath/LaunchAgents")" || err=$? 102if (( err )); then 103oldDir="" 104fi 105fi 106newDir= ${ escapeShellArg agentsDrv } 107dstDir= ${ escapeShellArg dstDir } 108 109local oldSrcPath newSrcPath dstPath agentFile agentName 110 111find -L "$newDir" -maxdepth 1 -name '*.plist' -type f -print0 \ 112| while IFS= read -rd "" newSrcPath; do 113agentFile="''${newSrcPath##*/}" 114agentName="''${agentFile%.plist}" 115dstPath="$dstDir/$agentFile" 116oldSrcPath="$oldDir/$agentFile" 117 118if [[ ! -e "$dstPath" ]]; then 119continue 120fi 121 122if ! cmp --quiet "$oldSrcPath" "$dstPath"; then 123errorEcho "Existing file '$dstPath' is in the way of '$newSrcPath'" 124exit 1 125fi 126done 127} 128 129checkLaunchAgents 130'' ; 131 132# NOTE: Launch Agent configurations can't be symlinked from the Nix store 133# because it needs to be owned by the user running it. 134home . activation . setupLaunchAgents = 135hm . dag . entryAfter [ "writeBoundary" ] '' 136setupLaunchAgents() { 137local oldDir newDir dstDir domain err 138oldDir="" 139err=0 140if [[ -n "''${oldGenPath:-}" ]]; then 141oldDir="$(readlink -m "$oldGenPath/LaunchAgents")" || err=$? 142if (( err )); then 143oldDir="" 144fi 145fi 146newDir="$(readlink -m "$newGenPath/LaunchAgents")" 147dstDir= ${ escapeShellArg dstDir } 148domain="gui/$UID" 149err=0 150 151local srcPath dstPath agentFile agentName i bootout_retries 152bootout_retries=10 153 154find -L "$newDir" -maxdepth 1 -name '*.plist' -type f -print0 \ 155| while IFS= read -rd "" srcPath; do 156agentFile="''${srcPath##*/}" 157agentName="''${agentFile%.plist}" 158dstPath="$dstDir/$agentFile" 159 160if cmp --quiet "$srcPath" "$dstPath"; then 161continue 162fi 163if [[ -f "$dstPath" ]]; then 164for (( i = 0; i < bootout_retries; i++ )); do 165$DRY_RUN_CMD /bin/launchctl bootout "$domain/$agentName" || err=$? 166if [[ -v DRY_RUN ]]; then 167break 168fi 169if (( err != 9216 )) && 170! /bin/launchctl print "$domain/$agentName" &> /dev/null; then 171break 172fi 173sleep 1 174done 175if (( i == bootout_retries )); then 176warnEcho "Failed to stop '$domain/$agentName'" 177return 1 178fi 179fi 180$DRY_RUN_CMD install -Dm444 -T "$srcPath" "$dstPath" 181$DRY_RUN_CMD /bin/launchctl bootstrap "$domain" "$dstPath" 182done 183 184if [[ ! -e "$oldDir" ]]; then 185return 186fi 187 188find -L "$oldDir" -maxdepth 1 -name '*.plist' -type f -print0 \ 189| while IFS= read -rd "" srcPath; do 190agentFile="''${srcPath##*/}" 191agentName="''${agentFile%.plist}" 192dstPath="$dstDir/$agentFile" 193if [[ -e "$newDir/$agentFile" ]]; then 194continue 195fi 196 197$DRY_RUN_CMD /bin/launchctl bootout "$domain/$agentName" || : 198if [[ ! -e "$dstPath" ]]; then 199continue 200fi 201if ! cmp --quiet "$srcPath" "$dstPath"; then 202warnEcho "Skipping deletion of '$dstPath', since its contents have diverged" 203continue 204fi 205$DRY_RUN_CMD rm -f $VERBOSE_ARG "$dstPath" 206done 207} 208 209setupLaunchAgents 210'' ; 211}) 212]; 213}