1# idea: provide a build environments for your development of preference
2/*
3 #### examples of use: ####
4 # Add this to your ~/.config/nixpkgs/config.nix:
5 {
6 packageOverrides = pkgs : with pkgs; {
7 sdlEnv = pkgs.myEnvFun {
8 name = "sdl";
9 nativeBuildInputs = [ cmake pkg-config ];
10 buildInputs = [ stdenv SDL SDL_image SDL_ttf SDL_gfx SDL_net];
11 };
12 };
13 }
14
15 # Then you can install it by:
16 # $ nix-env -i env-sdl
17 # And you can load it simply calling:
18 # $ load-env-sdl
19 # and this will update your env vars to have 'make' and 'gcc' finding the SDL
20 # headers and libs.
21
22 ##### Another example, more complicated but achieving more: #######
23 # Make an environment to build nix from source and create ctags (tagfiles can
24 # be extracted from TAG_FILES) from every source package. Here would be a
25 # full ~/.config/nixpkgs/config.nix
26 {
27 packageOverrides = pkgs : with pkgs; with sourceAndTags;
28 let complicatedMyEnv = { name, buildInputs ? [], cTags ? [], extraCmds ? ""}:
29 pkgs.myEnvFun {
30 inherit name;
31 buildInputs = buildInputs
32 ++ map (x : sourceWithTagsDerivation
33 ( (addCTaggingInfo x ).passthru.sourceWithTags ) ) cTags;
34 extraCmds = ''
35 ${extraCmds}
36 HOME=${builtins.getEnv "HOME"}
37 . ~/.bashrc
38 '';
39 };
40 in rec {
41 # this is the example we will be using
42 nixEnv = complicatedMyEnv {
43 name = "nix";
44 buildInputs = [ libtool stdenv perl curl bzip2 openssl db5 autoconf automake zlib ];
45 };
46 };
47 }
48
49 # Now we should build our newly defined custom environment using this command on a shell, so type:
50 # $ nix-env -i env-nix
51
52 # You can load the environment simply typing a "load-env-${name}" command.
53 # $ load-env-nix
54 # The result using that command should be:
55 # env-nix loaded
56 and show you a shell with a prefixed prompt.
57*/
58
59{
60 mkDerivation,
61 replaceVars,
62 pkgs,
63}:
64{
65 stdenv ? pkgs.stdenv,
66 name,
67 buildInputs ? [ ],
68 propagatedBuildInputs ? [ ],
69 extraCmds ? "",
70 cleanupCmds ? "",
71 shell ? "${pkgs.bashInteractive}/bin/bash --norc",
72}:
73
74mkDerivation {
75 inherit buildInputs propagatedBuildInputs;
76
77 name = "env-${name}";
78 phases = [
79 "buildPhase"
80 "fixupPhase"
81 ];
82 setupNew = ../../stdenv/generic/setup.sh;
83
84 buildPhase =
85 let
86 initialPath = import ../../stdenv/generic/common-path.nix { inherit pkgs; };
87 in
88 ''
89 set -x
90 mkdir -p "$out/dev-envs" "$out/nix-support" "$out/bin"
91 s="$out/nix-support/setup-new-modified"
92 # shut some warning up.., do not use set -e
93 sed -e 's@set -eu@@' \
94 -e 's@assertEnvExists\s\+NIX_STORE@:@' \
95 -e 's@trap.*@@' \
96 -e '1i initialPath="${toString initialPath}"' \
97 "$setupNew" > "$s"
98 cat >> "$out/dev-envs/''${name/env-/}" << EOF
99 defaultNativeBuildInputs="$defaultNativeBuildInputs"
100 buildInputs="$buildInputs"
101 propagatedBuildInputs="$propagatedBuildInputs"
102 # the setup-new script wants to write some data to a temp file.. so just let it do that and tidy up afterwards
103 tmp="\$("${pkgs.coreutils}/bin/mktemp" -d)"
104 NIX_BUILD_TOP="\$tmp"
105 phases=
106 # only do all the setup stuff in nix-support/*
107 set +e
108 # This prevents having -rpath /lib in NIX_LDFLAGS
109 export NIX_NO_SELF_RPATH=1
110 if [[ -z "\$ZSH_VERSION" ]]; then
111 source "$s"
112 else
113 setopt interactivecomments
114 # fix bash indirection
115 # let's hope the bash arrays aren't used
116 # substitute is using bash array, so skip it
117 echo '
118 setopt NO_BAD_PATTERN
119 setopt NO_BANG_HIST
120 setopt NO_BG_NICE
121 setopt NO_EQUALS
122 setopt NO_FUNCTION_ARGZERO
123 setopt GLOB_SUBST
124 setopt NO_HUP
125 setopt INTERACTIVE_COMMENTS
126 setopt KSH_ARRAYS
127 setopt NO_MULTIOS
128 setopt NO_NOMATCH
129 setopt RM_STAR_SILENT
130 setopt POSIX_BUILTINS
131 setopt SH_FILE_EXPANSION
132 setopt SH_GLOB
133 setopt SH_OPTION_LETTERS
134 setopt SH_WORD_SPLIT
135 ' >> "\$tmp/script"
136 sed -e 's/\''${!\([^}]*\)}/\''${(P)\1}/g' \
137 -e 's/[[]\*\]//' \
138 -e 's/substitute() {/ substitute() { return; /' \
139 -e 's@PATH=\$@PATH=${pkgs.coreutils}/bin@' \
140 "$s" >> "\$tmp/script"
141 echo "\$tmp/script";
142 source "\$tmp/script";
143 fi
144 ${pkgs.coreutils}/bin/rm -fr "\$tmp"
145 ${extraCmds}
146
147 nix_cleanup() {
148 :
149 ${cleanupCmds}
150 }
151
152 export PATH
153 echo $name loaded >&2
154
155 trap nix_cleanup EXIT
156 EOF
157
158 mkdir -p $out/bin
159 sed -e 's,@shell@,${shell},' -e s,@myenvpath@,$out/dev-envs/${name}, \
160 -e 's,@name@,${name},' ${./loadenv.sh} > $out/bin/load-env-${name}
161 chmod +x $out/bin/load-env-${name}
162 '';
163}