forked from
lass.pds.lassul.us/superconfig
This repository has no description
3.3 kB
124 lines
1{ lib, pkgs, ... }:
2{
3 /**
4 Create a wrapped application that preserves all original outputs (man pages, completions, etc.)
5
6 # Arguments
7
8 - `package`: The package to wrap
9 - `runtimeInputs`: List of packages to add to PATH (optional)
10 - `env`: Attribute set of environment variables to export (optional)
11 - `flags`: Attribute set of command-line flags to add (optional)
12 - `preHook`: Shell script to run before executing the command (optional)
13 - `wrapper`: Custom wrapper function (optional, defaults to exec'ing the original binary with flags)
14 - Called with { env, flags, envString, flagsString, exePath, preHook }
15
16 # Example
17
18 ```nix
19 makeWrapper pkgs.curl {
20 runtimeInputs = [ pkgs.jq ];
21 env = {
22 CURL_CA_BUNDLE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
23 };
24 flags = {
25 "--silent" = { }; # becomes --silent
26 "--connect-timeout" = "30"; # becomes --connect-timeout 30
27 };
28 preHook = ''
29 echo "Making request..." >&2
30 '';
31 }
32
33 # Or with custom wrapper:
34 makeWrapper pkgs.someProgram {
35 wrapper = { exePath, flagsString, envString, preHook, ... }: ''
36 ${envString}
37 ${preHook}
38 echo "Custom logic here"
39 exec ${exePath} ${flagsString} "$@"
40 '';
41 }
42 ```
43 */
44 makeWrapper =
45 package:
46 {
47 runtimeInputs ? [ ],
48 env ? { },
49 flags ? { },
50 preHook ? "",
51 wrapper ? (
52 {
53 exePath,
54 flagsString,
55 envString,
56 preHook,
57 ...
58 }:
59 ''
60 ${envString}
61 ${preHook}
62 exec ${exePath}${flagsString} "$@"
63 ''
64 ),
65 }:
66 let
67 # Extract binary name from the exe path
68 exePath = lib.getExe package;
69 binName = baseNameOf exePath;
70
71 # Generate environment variable exports
72 envString =
73 if env == { } then
74 ""
75 else
76 lib.concatStringsSep "\n" (
77 lib.mapAttrsToList (name: value: ''export ${name}="${toString value}"'') env
78 )
79 + "\n";
80
81 # Generate flag arguments with proper line breaks and indentation
82 flagsString =
83 if flags == { } then
84 ""
85 else
86 " \\\n "
87 + lib.concatStringsSep " \\\n " (
88 lib.mapAttrsToList (
89 name: value: if value == { } then "${name}" else "${name} ${lib.escapeShellArg (toString value)}"
90 ) flags
91 );
92
93 finalWrapper = wrapper {
94 inherit
95 env
96 flags
97 envString
98 flagsString
99 exePath
100 preHook
101 ;
102 };
103 in
104 pkgs.symlinkJoin {
105 name = package.pname or package.name;
106 paths = [
107 (pkgs.writeShellApplication {
108 name = binName;
109 runtimeInputs = runtimeInputs;
110 text = finalWrapper;
111 })
112 package
113 ];
114 passthru = (package.passthru or { }) // {
115 inherit env flags preHook;
116 };
117 # Pass through original attributes
118 inherit (package) meta;
119 } // lib.optionalAttrs (package ? version) {
120 inherit (package) version;
121 } // lib.optionalAttrs (package ? pname) {
122 inherit (package) pname;
123 };
124}