fork/home-manager
git clone https://git.julia.blue/fork/home-manager
c3ab5ea0
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7inherit ( config . home ) stateVersion ; 8 9cfg = config . home ; 10 11languageSubModule = types . submodule { 12options = { 13base = mkOption { 14default = null ; 15type = types . nullOr types . str ; 16description = '' 17The language to use unless overridden by a more specific option. 18'' ; 19}; 20 21ctype = mkOption { 22default = null ; 23type = types . nullOr types . str ; 24description = '' 25Character classification category. 26'' ; 27}; 28 29numeric = mkOption { 30default = null ; 31type = types . nullOr types . str ; 32description = '' 33The language to use for numerical values. 34'' ; 35}; 36 37time = mkOption { 38default = null ; 39type = types . nullOr types . str ; 40description = '' 41The language to use for formatting times. 42'' ; 43}; 44 45collate = mkOption { 46default = null ; 47type = types . nullOr types . str ; 48description = '' 49The language to use for collation (alphabetical ordering). 50'' ; 51}; 52 53monetary = mkOption { 54default = null ; 55type = types . nullOr types . str ; 56description = '' 57The language to use for formatting currencies and money amounts. 58'' ; 59}; 60 61messages = mkOption { 62default = null ; 63type = types . nullOr types . str ; 64description = '' 65The language to use for messages, application UI languages, etc. 66'' ; 67}; 68 69paper = mkOption { 70default = null ; 71type = types . nullOr types . str ; 72description = '' 73The language to use for paper sizes. 74'' ; 75}; 76 77name = mkOption { 78default = null ; 79type = types . nullOr types . str ; 80description = '' 81The language to use for personal names. 82'' ; 83}; 84 85address = mkOption { 86default = null ; 87type = types . nullOr types . str ; 88description = '' 89The language to use for addresses. 90'' ; 91}; 92 93telephone = mkOption { 94default = null ; 95type = types . nullOr types . str ; 96description = '' 97The language to use for telephone numbers. 98'' ; 99}; 100 101measurement = mkOption { 102default = null ; 103type = types . nullOr types . str ; 104description = '' 105The language to use for measurement values. 106'' ; 107}; 108 109}; 110}; 111 112keyboardSubModule = types . submodule { 113options = { 114layout = mkOption { 115type = with types ; nullOr str ; 116default = 117if versionAtLeast config . home . stateVersion "19.09" 118then null 119else "us" ; 120defaultText = literalExpression "null" ; 121description = '' 122Keyboard layout. If `null`, then the system 123configuration will be used. 124 125This defaults to `null` for state 126version ≥ 19.09 and `"us"` otherwise. 127'' ; 128}; 129 130model = mkOption { 131type = with types ; nullOr str ; 132default = null ; 133example = "presario" ; 134description = '' 135Keyboard model. 136'' ; 137}; 138 139options = mkOption { 140type = types . listOf types . str ; 141default = []; 142example = [ "grp:caps_toggle" "grp_led:scroll" ]; 143description = '' 144X keyboard options; layout switching goes here. 145'' ; 146}; 147 148variant = mkOption { 149type = with types ; nullOr str ; 150default = 151if versionAtLeast config . home . stateVersion "19.09" 152then null 153else "" ; 154defaultText = literalExpression "null" ; 155example = "colemak" ; 156description = '' 157X keyboard variant. If `null`, then the 158system configuration will be used. 159 160This defaults to `null` for state 161version ≥ 19.09 and `""` otherwise. 162'' ; 163}; 164}; 165}; 166 167in 168 169{ 170meta . maintainers = [ maintainers . rycee ]; 171 172imports = [ 173( mkRemovedOptionModule [ "home" "sessionVariableSetter" ] '' 174Session variables are now always set through the shell. This is 175done automatically if the shell configuration is managed by Home 176Manager. If not, then you must source the 177 178~/.nix-profile/etc/profile.d/hm-session-vars.sh 179 180file yourself. 181'' ) 182]; 183 184options = { 185home . username = mkOption { 186type = types . str ; 187defaultText = literalExpression '' 188"$USER" for state version < 20.09, 189undefined for state version ≥ 20.09 190'' ; 191example = "jane.doe" ; 192description = "The user's username." ; 193}; 194 195home . homeDirectory = mkOption { 196type = types . path ; 197defaultText = literalExpression '' 198"$HOME" for state version < 20.09, 199undefined for state version ≥ 20.09 200'' ; 201apply = toString ; 202example = "/home/jane.doe" ; 203description = "The user's home directory. Must be an absolute path." ; 204}; 205 206home . profileDirectory = mkOption { 207type = types . path ; 208defaultText = literalExpression '' 209"''${home.homeDirectory}/.nix-profile" or 210"/etc/profiles/per-user/''${home.username}" 211'' ; 212readOnly = true ; 213description = '' 214The profile directory where Home Manager generations are installed. 215'' ; 216}; 217 218home . language = mkOption { 219type = languageSubModule ; 220default = {}; 221description = "Language configuration." ; 222}; 223 224home . keyboard = mkOption { 225type = types . nullOr keyboardSubModule ; 226default = if versionAtLeast stateVersion "21.11" then null else { }; 227defaultText = literalExpression '' 228"{ }" for state version < 21.11, 229"null" for state version ≥ 21.11 230'' ; 231description = '' 232Keyboard configuration. Set to `null` to 233disable Home Manager keyboard management. 234'' ; 235}; 236 237home . shellAliases = mkOption { 238type = with types ; attrsOf str ; 239default = { }; 240example = literalExpression '' 241{ 242g = "git"; 243"..." = "cd ../.."; 244} 245'' ; 246description = '' 247An attribute set that maps aliases (the top level attribute names 248in this option) to command strings or directly to build outputs. 249 250This option should only be used to manage simple aliases that are 251compatible across all shells. If you need to use a shell specific 252feature then make sure to use a shell specific option, for example 253[](#opt-programs.bash.shellAliases) for Bash. 254'' ; 255}; 256 257home . sessionVariables = mkOption { 258default = {}; 259type = with types ; lazyAttrsOf ( oneOf [ str path int float ]); 260example = { EDITOR = "emacs" ; GS_OPTIONS = "-sPAPERSIZE=a4" ; }; 261description = '' 262Environment variables to always set at login. 263 264The values may refer to other environment variables using 265POSIX.2 style variable references. For example, a variable 266{var}`parameter` may be referenced as 267`$parameter` or `''${parameter}`. A 268default value `foo` may be given as per 269`''${parameter:-foo}` and, similarly, an alternate 270value `bar` can be given as per 271`''${parameter:+bar}`. 272 273Note, these variables may be set in any order so no session 274variable may have a runtime dependency on another session 275variable. In particular code like 276```nix 277home.sessionVariables = { 278FOO = "Hello"; 279BAR = "$FOO World!"; 280}; 281``` 282may not work as expected. If you need to reference another 283session variable, then do so inside Nix instead. The above 284example then becomes 285```nix 286home.sessionVariables = { 287FOO = "Hello"; 288BAR = "''${config.home.sessionVariables.FOO} World!"; 289}; 290``` 291'' ; 292}; 293 294home . sessionVariablesPackage = mkOption { 295type = types . package ; 296internal = true ; 297description = '' 298The package containing the 299{file}`hm-session-vars.sh` file. 300'' ; 301}; 302 303home . sessionPath = mkOption { 304type = with types ; listOf str ; 305default = [ ]; 306example = [ 307"$HOME/.local/bin" 308"\${xdg.configHome}/emacs/bin" 309".git/safe/../../bin" 310]; 311description = '' 312Extra directories to add to {env}`PATH`. 313 314These directories are added to the {env}`PATH` variable in a 315double-quoted context, so expressions like `$HOME` are 316expanded by the shell. However, since expressions like `~` or 317`*` are escaped, they will end up in the {env}`PATH` 318verbatim. 319'' ; 320}; 321 322home . sessionVariablesExtra = mkOption { 323type = types . lines ; 324default = "" ; 325internal = true ; 326description = '' 327Extra configuration to add to the 328{file}`hm-session-vars.sh` file. 329'' ; 330}; 331 332home . packages = mkOption { 333type = types . listOf types . package ; 334default = []; 335description = "The set of packages to appear in the user environment." ; 336}; 337 338home . extraOutputsToInstall = mkOption { 339type = types . listOf types . str ; 340default = []; 341example = [ "doc" "info" "devdoc" ]; 342description = '' 343List of additional package outputs of the packages 344{var}`home.packages` that should be installed into 345the user environment. 346'' ; 347}; 348 349home . path = mkOption { 350internal = true ; 351description = "The derivation installing the user packages." ; 352}; 353 354home . emptyActivationPath = mkOption { 355internal = true ; 356type = types . bool ; 357default = versionAtLeast stateVersion "22.11" ; 358defaultText = literalExpression '' 359false for state version < 22.11, 360true for state version ≥ 22.11 361'' ; 362description = '' 363Whether the activation script should start with an empty 364{env}`PATH` variable. When `false` then the 365user's {env}`PATH` will be accessible in the script. It is 366recommended to keep this at `true` to avoid 367uncontrolled use of tools found in PATH. 368'' ; 369}; 370 371home . activation = mkOption { 372type = hm . types . dagOf types . str ; 373default = {}; 374example = literalExpression '' 375{ 376myActivationAction = lib.hm.dag.entryAfter ["writeBoundary"] ''' 377$DRY_RUN_CMD ln -s $VERBOSE_ARG \ 378''${builtins.toPath ./link-me-directly} $HOME 379'''; 380} 381'' ; 382description = '' 383The activation scripts blocks to run when activating a Home 384Manager generation. Any entry here should be idempotent, 385meaning running twice or more times produces the same result 386as running it once. 387 388If the script block produces any observable side effect, such 389as writing or deleting files, then it 390*must* be placed after the special 391`writeBoundary` script block. Prior to the 392write boundary one can place script blocks that verifies, but 393does not modify, the state of the system and exits if an 394unexpected state is found. For example, the 395`checkLinkTargets` script block checks for 396collisions between non-managed files and files defined in 397[](#opt-home.file). 398 399A script block should respect the {var}`DRY_RUN` 400variable, if it is set then the actions taken by the script 401should be logged to standard out and not actually performed. 402The variable {var}`DRY_RUN_CMD` is set to 403{command}`echo` if dry run is enabled. 404 405A script block should also respect the 406{var}`VERBOSE` variable, and if set print 407information on standard out that may be useful for debugging 408any issue that may arise. The variable 409{var}`VERBOSE_ARG` is set to 410{option}`--verbose` if verbose output is enabled. 411'' ; 412}; 413 414home . activationPackage = mkOption { 415internal = true ; 416type = types . package ; 417description = "The package containing the complete activation script." ; 418}; 419 420home . extraActivationPath = mkOption { 421internal = true ; 422type = types . listOf types . package ; 423default = [ ]; 424description = '' 425Extra packages to add to {env}`PATH` within the activation 426script. 427'' ; 428}; 429 430home . extraBuilderCommands = mkOption { 431type = types . lines ; 432default = "" ; 433internal = true ; 434description = '' 435Extra commands to run in the Home Manager generation builder. 436'' ; 437}; 438 439home . extraProfileCommands = mkOption { 440type = types . lines ; 441default = "" ; 442internal = true ; 443description = '' 444Extra commands to run in the Home Manager profile builder. 445'' ; 446}; 447 448home . enableNixpkgsReleaseCheck = mkOption { 449type = types . bool ; 450default = true ; 451description = '' 452Determines whether to check for release version mismatch between Home 453Manager and Nixpkgs. Using mismatched versions is likely to cause errors 454and unexpected behavior. It is therefore highly recommended to use a 455release of Home Manager that corresponds with your chosen release of 456Nixpkgs. 457 458When this option is enabled and a mismatch is detected then a warning 459will be printed when the user configuration is being built. 460'' ; 461}; 462}; 463 464config = { 465assertions = [ 466{ 467assertion = config . home . username != "" ; 468message = "Username could not be determined" ; 469} 470{ 471assertion = config . home . homeDirectory != "" ; 472message = "Home directory could not be determined" ; 473} 474]; 475 476warnings = 477let 478hmRelease = config . home . version . release ; 479nixpkgsRelease = lib . trivial . release ; 480releaseMismatch = 481config . home . enableNixpkgsReleaseCheck 482&& hmRelease != nixpkgsRelease ; 483in 484optional releaseMismatch '' 485You are using 486 487Home Manager version ${ hmRelease } and 488Nixpkgs version ${ nixpkgsRelease } . 489 490Using mismatched versions is likely to cause errors and unexpected 491behavior. It is therefore highly recommended to use a release of Home 492Manager that corresponds with your chosen release of Nixpkgs. 493 494If you insist then you can disable this warning by adding 495 496home.enableNixpkgsReleaseCheck = false; 497 498to your configuration. 499'' ; 500 501home . username = 502mkIf ( versionOlder config . home . stateVersion "20.09" ) 503( mkDefault ( builtins . getEnv "USER" )); 504home . homeDirectory = 505mkIf ( versionOlder config . home . stateVersion "20.09" ) 506( mkDefault ( builtins . getEnv "HOME" )); 507 508home . profileDirectory = 509if config . submoduleSupport . enable 510&& config . submoduleSupport . externalPackageInstall 511then "/etc/profiles/per-user/ ${ cfg . username } " 512else if config . nix . enable && ( config . nix . settings . use-xdg-base-directories or false ) 513then " ${ config . xdg . stateHome } /nix/profile" 514else cfg . homeDirectory + "/.nix-profile" ; 515 516programs . bash . shellAliases = cfg . shellAliases ; 517programs . zsh . shellAliases = cfg . shellAliases ; 518programs . fish . shellAliases = cfg . shellAliases ; 519 520home . sessionVariables = 521let 522maybeSet = n :v :optionalAttrs ( v != null ) { ${ n } = v ; }; 523in 524( maybeSet "LANG" cfg . language . base ) 525// 526( maybeSet "LC_CTYPE" cfg . language . ctype ) 527// 528( maybeSet "LC_NUMERIC" cfg . language . numeric ) 529// 530( maybeSet "LC_TIME" cfg . language . time ) 531// 532( maybeSet "LC_COLLATE" cfg . language . collate ) 533// 534( maybeSet "LC_MONETARY" cfg . language . monetary ) 535// 536( maybeSet "LC_MESSAGES" cfg . language . messages ) 537// 538( maybeSet "LC_PAPER" cfg . language . paper ) 539// 540( maybeSet "LC_NAME" cfg . language . name ) 541// 542( maybeSet "LC_ADDRESS" cfg . language . address ) 543// 544( maybeSet "LC_TELEPHONE" cfg . language . telephone ) 545// 546( maybeSet "LC_MEASUREMENT" cfg . language . measurement ); 547 548# Provide a file holding all session variables. 549home . sessionVariablesPackage = pkgs . writeTextFile { 550name = "hm-session-vars.sh" ; 551destination = "/etc/profile.d/hm-session-vars.sh" ; 552text = '' 553# Only source this once. 554if [ -n "$__HM_SESS_VARS_SOURCED" ]; then return; fi 555export __HM_SESS_VARS_SOURCED=1 556 557${ config . lib . shell . exportAll cfg . sessionVariables } 558'' + lib . optionalString ( cfg . sessionPath != [ ]) '' 559export PATH="$PATH''${PATH:+:} ${ concatStringsSep ":" cfg . sessionPath } " 560'' + cfg . sessionVariablesExtra ; 561}; 562 563home . packages = [ config . home . sessionVariablesPackage ]; 564 565# A dummy entry acting as a boundary between the activation 566# script's "check" and the "write" phases. 567home . activation . writeBoundary = hm . dag . entryAnywhere "" ; 568 569# Install packages to the user environment. 570# 571# Note, sometimes our target may not allow modification of the Nix 572# store and then we cannot rely on `nix-env -i`. This is the case, 573# for example, if we are running as a NixOS module and building a 574# virtual machine. Then we must instead rely on an external 575# mechanism for installing packages, which in NixOS is provided by 576# the `users.users.<name?>.packages` option. The activation 577# command is still needed since some modules need to run their 578# activation commands after the packages are guaranteed to be 579# installed. 580# 581# In case the user has moved from a user-install of Home Manager 582# to a submodule managed one we attempt to uninstall the 583# `home-manager-path` package if it is installed. 584home . activation . installPackages = hm . dag . entryAfter [ "writeBoundary" ] ( 585if config . submoduleSupport . externalPackageInstall 586then 587'' 588if [[ -e $HOME/.nix-profile/manifest.json ]] ; then 589nix profile list \ 590| { grep 'home-manager-path$' || test $? = 1; } \ 591| cut -d ' ' -f 4 \ 592| xargs -t $DRY_RUN_CMD nix profile remove $VERBOSE_ARG 593else 594if nix-env -q | grep '^home-manager-path$'; then 595$DRY_RUN_CMD nix-env -e home-manager-path 596fi 597fi 598'' 599else 600'' 601function nixProfileList() { 602# We attempt to use `--json` first (added in Nix 2.17). Otherwise attempt to 603# parse the legacy output format. 604{ 605nix profile list --json 2>/dev/null \ 606| jq -r --arg name "$1" '.elements[].storePaths[] | select(endswith($name))' 607} || { 608nix profile list \ 609| { grep "$1\$" || test $? = 1; } \ 610| cut -d ' ' -f 4 611} 612} 613 614function nixRemoveProfileByName() { 615nixProfileList "$1" | xargs -t $DRY_RUN_CMD nix profile remove $VERBOSE_ARG 616} 617 618function nixReplaceProfile() { 619local oldNix="$(command -v nix)" 620 621nixRemoveProfileByName 'home-manager-path' 622 623$DRY_RUN_CMD $oldNix profile install $1 624} 625 626if [[ -e $HOME/.nix-profile/manifest.json ]] ; then 627INSTALL_CMD="nix profile install" 628INSTALL_CMD_ACTUAL="nixReplaceProfile" 629LIST_CMD="nix profile list" 630REMOVE_CMD_SYNTAX='nix profile remove {number | store path}' 631else 632INSTALL_CMD="nix-env -i" 633INSTALL_CMD_ACTUAL="$DRY_RUN_CMD nix-env -i" 634LIST_CMD="nix-env -q" 635REMOVE_CMD_SYNTAX='nix-env -e {package name}' 636fi 637 638if ! $INSTALL_CMD_ACTUAL ${ cfg . path } ; then 639echo 640_iError $'Oops, Nix failed to install your new Home Manager profile!\n\nPerhaps there is a conflict with a package that was installed using\n"%s"? Try running\n\n %s\n\nand if there is a conflicting package you can remove it with\n\n %s\n\nThen try activating your Home Manager configuration again.' "$INSTALL_CMD" "$LIST_CMD" "$REMOVE_CMD_SYNTAX" 641exit 1 642fi 643unset -f nixProfileList nixRemoveProfileByName nixReplaceProfile 644unset INSTALL_CMD INSTALL_CMD_ACTUAL LIST_CMD REMOVE_CMD_SYNTAX 645'' 646); 647 648# Text containing Bash commands that will initialize the Home Manager Bash 649# library. Most importantly, this will prepare for using translated strings 650# in the `hm-modules` text domain. 651lib . bash . initHomeManagerLib = 652let 653domainDir = pkgs . runCommand "hm-modules-messages" { 654nativeBuildInputs = [ pkgs . buildPackages . gettext ]; 655} '' 656for path in ${ ./po } /*.po ; do 657lang ="'' $ { path ##*/}" 658lang ="'' $ { lang %%.*}" 659mkdir -p " $ out / $ lang/LC_MESSAGES" 660msgfmt -o " $ out/ $ lang /LC_MESSAGES/hm-modules.mo" " $ path " 661done 662'' ; 663in 664'' 665export TEXTDOMAIN=hm-modules 666export TEXTDOMAINDIR= ${ domainDir } 667source ${ ../lib/bash/home-manager.sh } 668'' ; 669 670home . activationPackage = 671let 672mkCmd = res :'' 673_iNote "Activating %s" " ${ res . name } " 674${ res . data } 675'' ; 676sortedCommands = hm . dag . topoSort cfg . activation ; 677activationCmds = 678if sortedCommands ?result then 679concatStringsSep "\n" ( map mkCmd sortedCommands . result ) 680else 681abort ( "Dependency cycle in activation script: " 682+ builtins . toJSON sortedCommands ); 683 684# Programs that always should be available on the activation 685# script's PATH. 686activationBinPaths = lib . makeBinPath ( 687with pkgs ; [ 688bash 689coreutils 690diffutils # For `cmp` and `diff`. 691findutils 692gettext 693gnugrep 694gnused 695jq 696ncurses # For `tput`. 697] 698++ config . home . extraActivationPath 699) 700+ ( 701# Add path of the Nix binaries, if a Nix package is configured, then 702# use that one, otherwise grab the path of the nix-env tool. 703if config . nix . enable && config . nix . package != null then 704": ${ config . nix . package } /bin" 705else 706":$( ${ pkgs . coreutils } /bin/dirname $( ${ pkgs . coreutils } /bin/readlink -m $(type -p nix-env)))" 707) 708+ optionalString ( ! cfg . emptyActivationPath ) "\${PATH:+:}$PATH" ; 709 710activationScript = pkgs . writeShellScript "activation-script" '' 711set -eu 712set -o pipefail 713 714cd $ HOME 715 716export PATH =" ${ activationBinPaths } " 717${ config . lib . bash . initHomeManagerLib } 718 719${ builtins . readFile ./lib-bash/activation-init.sh } 720 721if [[ ! -v SKIP_SANITY_CHECKS ]]; then 722checkUsername ${ escapeShellArg config . home . username } 723checkHomeDirectory ${ escapeShellArg config . home . homeDirectory } 724fi 725 726${ activationCmds } 727'' ; 728in 729pkgs . runCommand 730"home-manager-generation" 731{ 732preferLocalBuild = true ; 733} 734'' 735mkdir -p $ out 736 737echo " ${ config . home . version . full } " > $ out /hm-version 738 739cp ${ activationScript } $ out /activate 740 741mkdir $ out /bin 742ln -s $ out /activate $ out /bin/home-manager-generation 743 744substituteInPlace $ out /activate \ 745--subst-var-by GENERATION_DIR $ out 746 747ln -s ${ config . home-files } $ out /home-files 748ln -s ${ cfg . path } $ out /home-path 749 750${ cfg . extraBuilderCommands } 751'' ; 752 753home . path = pkgs . buildEnv { 754name = "home-manager-path" ; 755 756paths = cfg . packages ; 757inherit ( cfg ) extraOutputsToInstall ; 758 759postBuild = cfg . extraProfileCommands ; 760 761meta = { 762description = "Environment of packages installed through home-manager" ; 763}; 764}; 765}; 766}