nixpkgs/pkgs/games/factorio/utils.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.7 KiB
Nix
Raw Normal View History

# This file provides a top-level function that will be used by both nixpkgs and nixos
# to generate mod directories for use at runtime by factorio.
2021-01-15 04:31:39 +00:00
{ lib, stdenv }:
with lib;
{
2022-10-31 17:57:07 +00:00
mkModDirDrv = mods: modsDatFile: # a list of mod derivations
let
recursiveDeps = modDrv: [modDrv] ++ map recursiveDeps modDrv.deps;
modDrvs = unique (flatten (map recursiveDeps mods));
in
stdenv.mkDerivation {
name = "factorio-mod-directory";
preferLocalBuild = true;
buildCommand = ''
mkdir -p $out
for modDrv in ${toString modDrvs}; do
# NB: there will only ever be a single zip file in each mod derivation's output dir
ln -s $modDrv/*.zip $out
done
2022-12-06 18:29:54 +00:00
'' + (lib.optionalString (modsDatFile != null) ''
2022-10-31 17:57:07 +00:00
cp ${modsDatFile} $out/mod-settings.dat
2022-12-06 18:29:54 +00:00
'');
};
modDrv = { allRecommendedMods, allOptionalMods }:
{ src
, name ? null
, deps ? []
, optionalDeps ? []
, recommendedDeps ? []
}: stdenv.mkDerivation {
inherit src;
# Use the name of the zip, but endstrip ".zip" and possibly the querystring that gets left in by fetchurl
name = replaceStrings ["_"] ["-"] (if name != null then name else removeSuffix ".zip" (head (splitString "?" src.name)));
deps = deps ++ optionals allOptionalMods optionalDeps
++ optionals allRecommendedMods recommendedDeps;
preferLocalBuild = true;
buildCommand = ''
mkdir -p $out
srcBase=$(basename $src)
srcBase=''${srcBase#*-} # strip nix hash
srcBase=''${srcBase%\?*} # strip querystring leftover from fetchurl
cp $src $out/$srcBase
'';
};
}