diff --git a/modules/build/activation.nix b/modules/build/activation.nix index 2817d83..ae50010 100644 --- a/modules/build/activation.nix +++ b/modules/build/activation.nix @@ -111,6 +111,12 @@ in internal = true; description = "Package containing /etc files."; }; + + sessionInit = mkOption { + type = types.path; + internal = true; + description = "File containing session init commands like exposing environment variables."; + }; }; }; diff --git a/modules/environment/login/login-inner.nix b/modules/environment/login/login-inner.nix index dea0abf..487e64c 100644 --- a/modules/environment/login/login-inner.nix +++ b/modules/environment/login/login-inner.nix @@ -17,10 +17,9 @@ writeText "login-inner" '' [ "$#" -gt 0 ] || echo "If nothing works, use the rescue shell and read ${config.build.installationDir}/usr/lib/login-inner" [ "$#" -gt 0 ] || echo "If it does not help, report bugs at https://github.com/t184256/nix-on-droid-bootstrap/issues" - export USER="${config.user.userName}" - export HOME="${config.user.home}" - - export GC_NPROCS=1 # to prevent gc warnings of nix, see https://github.com/NixOS/nix/issues/3237 + set +u + . "/nix/var/nix/profiles/per-user/$USER/profile/etc/profile.d/nix-on-droid-session-init.sh" + set -u ${lib.optionalString config.build.initialBuild '' if [ -e /etc/UNINTIALISED ]; then diff --git a/modules/environment/session-init.nix b/modules/environment/session-init.nix new file mode 100644 index 0000000..9c85103 --- /dev/null +++ b/modules/environment/session-init.nix @@ -0,0 +1,94 @@ +# Licensed under GNU Lesser General Public License v3 or later, see COPYING. +# Copyright (c) 2019 Alexander Sosedkin and other contributors, see AUTHORS. + +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.environment; + + export = n: v: "export ${n}=\"${toString v}\""; + + exportAll = vars: concatStringsSep "\n" (mapAttrsToList export vars); + + sessionInit = pkgs.writeTextFile { + name = "nix-on-droid-session-init.sh"; + destination = "/etc/profile.d/nix-on-droid-session-init.sh"; + text = '' + # Only source this once. + [ -n "$__NOD_SESS_INIT_SOURCED" ] && return + export __NOD_SESS_INIT_SOURCED=1 + + ${exportAll cfg.sessionVariables} + ''; + }; +in + +{ + + ###### interface + + options = { + + environment.sessionVariables = mkOption { + default = {}; + type = types.attrs; + example = { EDITOR = "emacs"; GS_OPTIONS = "-sPAPERSIZE=a4"; }; + description = '' + Environment variables to always set at login. + + The values may refer to other environment variables using + POSIX.2 style variable references. For example, a variable + parameter may be referenced as + $parameter or ''${parameter}. A + default value foo may be given as per + ''${parameter:-foo} and, similarly, an alternate + value bar can be given as per + ''${parameter:+bar}. + + Note, these variables may be set in any order so no session + variable may have a runtime dependency on another session + variable. In particular code like + + environment.sessionVariables = { + FOO = "Hello"; + BAR = "$FOO World!"; + }; + + may not work as expected. If you need to reference another + session variable, then do so inside Nix instead. The above + example then becomes + + environment.sessionVariables = { + FOO = "Hello"; + BAR = "''${config.environment.sessionVariables.FOO} World!"; + }; + + ''; + }; + + }; + + + ###### implementation + + config = { + + build = { inherit sessionInit; }; + + environment = { + packages = [ sessionInit ]; + + sessionVariables = { + HOME = config.user.home; + USER = config.user.userName; + + # To prevent gc warnings of nix, see https://github.com/NixOS/nix/issues/3237 + GC_NPROCS = 1; + }; + }; + + }; + +} diff --git a/modules/module-list.nix b/modules/module-list.nix index e851cc6..15ab01a 100644 --- a/modules/module-list.nix +++ b/modules/module-list.nix @@ -9,6 +9,7 @@ ./environment/links.nix ./environment/login ./environment/path.nix + ./environment/session-init.nix ./home-manager.nix ./user.nix ./version.nix diff --git a/pkgs/nix-directory.nix b/pkgs/nix-directory.nix index 1467782..6a6914c 100644 --- a/pkgs/nix-directory.nix +++ b/pkgs/nix-directory.nix @@ -18,7 +18,10 @@ let ]; prootTermuxClosure = closureInfo { - rootPaths = [ prootTermux ]; + rootPaths = [ + config.build.sessionInit + prootTermux + ]; }; in