10 , enableDFHack ? false
12 , enableSoundSense ? false
15 , enableStoneSense ? false
21 # General config options:
23 , enableTruetype ? null # defaults to 24, see init.txt
25 , enableTextMode ? false
27 # An attribute set of settings to override in data/init/*.txt.
28 # For example, `init.FOO = true;` is translated to `[FOO:YES]` in init.txt
30 # TODO world-gen.txt, interface.txt require special logic
34 dfhack_ = dfhack.override {
35 inherit enableStoneSense;
39 if builtins.isString theme
40 then builtins.getAttr theme themes
44 name = "dwarf-fortress-base-env-${dwarf-fortress.dfVersion}";
46 # These are in inverse order for first packages to override the next ones.
48 ++ lib.optional (theme != null) ptheme
49 ++ lib.optional enableDFHack dfhack_
50 ++ lib.optional enableSoundSense soundSense
51 ++ lib.optionals enableTWBT [ twbt.lib twbt.art ]
52 ++ [ dwarf-fortress ];
54 ignoreCollisions = true;
57 settings_ = lib.recursiveUpdate {
60 if enableTextMode then "TEXT"
61 else if enableTWBT then "TWBT"
62 else if stdenv.hostPlatform.isDarwin then "STANDARD" # https://www.bay12games.com/dwarves/mantisbt/view.php?id=11680
65 TRUETYPE = enableTruetype;
71 forEach = attrs: f: lib.concatStrings (lib.mapAttrsToList f attrs);
74 if lib.isBool v then if v then "YES" else "NO"
75 else if lib.isInt v then toString v
76 else if lib.isString v then v
77 else throw "dwarf-fortress: unsupported configuration value ${toString v}";
79 config = runCommand "dwarf-fortress-config" {
80 nativeBuildInputs = [ gawk ];
82 mkdir -p $out/data/init
86 if ! gawk -i inplace -v RS='\r?\n' '
87 { n += sub("\\[" ENVIRON["k"] ":[^]]*\\]", "[" ENVIRON["k"] ":" ENVIRON["v"] "]"); print }
90 echo "error: no setting named '$k' in $file" >&2
94 '' + forEach settings_ (file: kv: ''
95 file=data/init/${lib.escapeShellArg file}.txt
96 cp ${baseEnv}/"$file" "$out/$file"
97 '' + forEach kv (k: v: lib.optionalString (v != null) ''
98 export k=${lib.escapeShellArg k} v=${lib.escapeShellArg (toTxt v)}
100 '')) + lib.optionalString enableDFHack ''
104 orig_md5=$(< "${dwarf-fortress}/hash.md5.orig")
105 patched_md5=$(< "${dwarf-fortress}/hash.md5")
106 input_file="${dfhack_}/hack/symbols.xml"
107 output_file="$out/hack/symbols.xml"
109 echo "[DFHack Wrapper] Fixing Dwarf Fortress MD5:"
110 echo " Input: $input_file"
111 echo " Search: $orig_md5"
112 echo " Output: $output_file"
113 echo " Replace: $patched_md5"
115 substitute "$input_file" "$output_file" --replace "$orig_md5" "$patched_md5"
118 # This is a separate environment because the config files to modify may come
119 # from any of the paths in baseEnv.
121 name = "dwarf-fortress-env-${dwarf-fortress.dfVersion}";
122 paths = [ config baseEnv ];
123 ignoreCollisions = true;
127 lib.throwIf (enableTWBT && !enableDFHack) "dwarf-fortress: TWBT requires DFHack to be enabled"
128 lib.throwIf (enableStoneSense && !enableDFHack) "dwarf-fortress: StoneSense requires DFHack to be enabled"
129 lib.throwIf (enableTextMode && enableTWBT) "dwarf-fortress: text mode and TWBT are mutually exclusive"
131 stdenv.mkDerivation {
132 pname = "dwarf-fortress";
133 version = dwarf-fortress.dfVersion;
135 dfInit = substituteAll {
136 name = "dwarf-fortress-init";
137 src = ./dwarf-fortress-init.in;
140 if stdenv.isLinux then "libs/Dwarf_Fortress"
142 stdenv_shell = "${stdenv.shell}";
143 cp = "${coreutils}/bin/cp";
144 rm = "${coreutils}/bin/rm";
145 ln = "${coreutils}/bin/ln";
146 cat = "${coreutils}/bin/cat";
147 mkdir = "${coreutils}/bin/mkdir";
150 runDF = ./dwarf-fortress.in;
151 runDFHack = ./dfhack.in;
152 runSoundSense = ./soundSense.in;
155 inherit dwarf-fortress dwarf-therapist twbt env;
162 substitute $runDF $out/bin/dwarf-fortress \
163 --subst-var-by stdenv_shell ${stdenv.shell} \
165 chmod 755 $out/bin/dwarf-fortress
166 '' + lib.optionalString enableDFHack ''
167 substitute $runDFHack $out/bin/dfhack \
168 --subst-var-by stdenv_shell ${stdenv.shell} \
170 chmod 755 $out/bin/dfhack
171 '' + lib.optionalString enableSoundSense ''
172 substitute $runSoundSense $out/bin/soundsense \
173 --subst-var-by stdenv_shell ${stdenv.shell} \
174 --subst-var-by jre ${jdk.jre} \
176 chmod 755 $out/bin/soundsense
179 preferLocalBuild = true;
181 inherit (dwarf-fortress) meta;