From 4d32203d4daa4d8784a32b3487dc164d4fa7c2ef Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sat, 6 Jul 2024 15:25:18 +0800 Subject: [PATCH] lib: add functions for creating NixOS-like environments --- lib/env-specific/nixos.nix | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/env-specific/nixos.nix b/lib/env-specific/nixos.nix index 2320024d..36d92559 100644 --- a/lib/env-specific/nixos.nix +++ b/lib/env-specific/nixos.nix @@ -11,4 +11,34 @@ # Checks if the NixOS config is being built for a particular format. isFormat = config: format: (config.formatAttr or "") == format; + + # Create a separate environment similar to NixOS `system.path`. This is + # typically used to create isolated environments for custom desktop sessions + # which makes it possible to have them installed side-by-side with their own + # set of applications and everything (except for overlapping NixOS services + # that will just add them into the NixOS environment itself). + mkNixoslikeEnvironment = config: args: + pkgs.buildEnv { + inherit (args) paths name; + inherit (config.environment) pathsToLink extraOutputsToInstall; + ignoreCollisions = true; + postBuild = + '' + # Remove wrapped binaries, they shouldn't be accessible via PATH. + find $out/bin -maxdepth 1 -name ".*-wrapped" -type l -delete + + if [ -x $out/bin/glib-compile-schemas -a -w $out/share/glib-2.0/schemas ]; then + $out/bin/glib-compile-schemas $out/share/glib-2.0/schemas + fi + + ${config.environment.extraSetup} + ''; + }; + + # Given an environment (built with `pkgs.buildEnv`), create a systemd + # environment attrset meant to be used as part of the desktop service. + mkSystemdDesktopEnvironment = env: { + XDG_DATA_DIRS = "${env}/share\${XDG_DATA_DIRS:+:$XDG_DATA_DIRS}"; + XDG_CONFIG_DIRS = "${env}/etc/xdg\${XDG_CONFIG_DIRS:+:$XDG_CONFIG_DIRS}"; + }; }