1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00
home-manager/modules/lib/zsh.nix
Austin Horstman 5176d93c0a lib/zsh: fix shell escaping and integrate formatShellArrayContent
Replace naive string quoting with lib.escapeShellArg for proper shell escaping
of special characters, spaces, and quotes. Integrate with new formatShellArrayContent
for intelligent multi-line array formatting in zsh array definitions.

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
2025-09-27 13:18:01 -05:00

34 lines
1.1 KiB
Nix

{ lib }:
rec {
# Produces a Zsh shell like value
toZshValue =
v:
if builtins.isBool v then
if v then "true" else "false"
else if builtins.isString v then
lib.escapeShellArg v
else if builtins.isList v then
let
shell = import ./shell.nix { inherit lib; };
in
"(${shell.formatShellArrayContent (map toString v)})"
else
''"${toString v}"'';
# Produces a Zsh shell like definition statement
define = n: v: "${n}=${toZshValue v}";
# Given an attribute set containing shell variable names and their
# assignments, this function produces a string containing a definition
# statement for each set entry.
defineAll = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList define vars);
# Produces a Zsh shell like export statement
export = n: v: "export ${define n v}";
# Given an attribute set containing shell variable names and their
# assignments, this function produces a string containing an export
# statement for each set entry.
exportAll = vars: lib.concatStringsSep "\n" (lib.mapAttrsToList export vars);
}