1 # idea: provide a build environments for your developement of preference
3 #### examples of use: ####
4 # Add this to your ~/.config/nixpkgs/config.nix:
6 packageOverrides = pkgs : with pkgs; {
7 sdlEnv = pkgs.myEnvFun {
9 nativeBuildInputs = [ cmake pkg-config ];
10 buildInputs = [ stdenv SDL SDL_image SDL_ttf SDL_gfx SDL_net];
15 # Then you can install it by:
16 # $ nix-env -i env-sdl
17 # And you can load it simply calling:
19 # and this will update your env vars to have 'make' and 'gcc' finding the SDL
23 ##### Another example, more complicated but achieving more: #######
24 # Make an environment to build nix from source and create ctags (tagfiles can
25 # be extracted from TAG_FILES) from every source package. Here would be a
26 # full ~/.config/nixpkgs/config.nix
28 packageOverrides = pkgs : with pkgs; with sourceAndTags;
29 let complicatedMyEnv = { name, buildInputs ? [], cTags ? [], extraCmds ? ""}:
32 buildInputs = buildInputs
33 ++ map (x : sourceWithTagsDerivation
34 ( (addCTaggingInfo x ).passthru.sourceWithTags ) ) cTags;
37 HOME=${builtins.getEnv "HOME"}
42 # this is the example we will be using
43 nixEnv = complicatedMyEnv {
45 buildInputs = [ libtool stdenv perl curl bzip2 openssl db5 autoconf automake zlib ];
50 # Now we should build our newly defined custom environment using this command on a shell, so type:
51 # $ nix-env -i env-nix
53 # You can load the environment simply typing a "load-env-${name}" command.
55 # The result using that command should be:
57 and show you a shell with a prefixed prompt.
60 { mkDerivation, substituteAll, pkgs }:
61 { stdenv ? pkgs.stdenv, name, buildInputs ? []
62 , propagatedBuildInputs ? [], gcc ? stdenv.cc, extraCmds ? ""
63 , cleanupCmds ? "", shell ? "${pkgs.bashInteractive}/bin/bash --norc"}:
66 inherit buildInputs propagatedBuildInputs;
69 phases = [ "buildPhase" "fixupPhase" ];
70 setupNew = substituteAll {
71 src = ../../stdenv/generic/setup.sh;
76 initialPath = import ../../stdenv/generic/common-path.nix { inherit pkgs; };
79 mkdir -p "$out/dev-envs" "$out/nix-support" "$out/bin"
80 s="$out/nix-support/setup-new-modified"
81 # shut some warning up.., do not use set -e
82 sed -e 's@set -eu@@' \
83 -e 's@assertEnvExists\s\+NIX_STORE@:@' \
85 -e '1i initialPath="${toString initialPath}"' \
87 cat >> "$out/dev-envs/''${name/env-/}" << EOF
88 defaultNativeBuildInputs="$defaultNativeBuildInputs"
89 buildInputs="$buildInputs"
90 propagatedBuildInputs="$propagatedBuildInputs"
91 # the setup-new script wants to write some data to a temp file.. so just let it do that and tidy up afterwards
92 tmp="\$("${pkgs.coreutils}/bin/mktemp" -d)"
95 # only do all the setup stuff in nix-support/*
97 # This prevents having -rpath /lib in NIX_LDFLAGS
98 export NIX_NO_SELF_RPATH=1
99 if [[ -z "\$ZSH_VERSION" ]]; then
102 setopt interactivecomments
103 # fix bash indirection
104 # let's hope the bash arrays aren't used
105 # substitute is using bash array, so skip it
107 setopt NO_BAD_PATTERN
111 setopt NO_FUNCTION_ARGZERO
114 setopt INTERACTIVE_COMMENTS
118 setopt RM_STAR_SILENT
119 setopt POSIX_BUILTINS
120 setopt SH_FILE_EXPANSION
122 setopt SH_OPTION_LETTERS
125 sed -e 's/\''${!\([^}]*\)}/\''${(P)\1}/g' \
127 -e 's/substitute() {/ substitute() { return; /' \
128 -e 's@PATH=\$@PATH=${pkgs.coreutils}/bin@' \
129 "$s" >> "\$tmp/script"
131 source "\$tmp/script";
133 ${pkgs.coreutils}/bin/rm -fr "\$tmp"
142 echo $name loaded >&2
144 trap nix_cleanup EXIT
148 sed -e 's,@shell@,${shell},' -e s,@myenvpath@,$out/dev-envs/${name}, \
149 -e 's,@name@,${name},' ${./loadenv.sh} > $out/bin/load-env-${name}
150 chmod +x $out/bin/load-env-${name}