2021-01-19 06:50:56 +00:00
|
|
|
{ stdenv, lib, crystal, shards, git, pkg-config, which, linkFarm, fetchFromGitHub, installShellFiles }:
|
2020-03-18 02:47:24 +00:00
|
|
|
|
2020-12-22 04:08:30 +00:00
|
|
|
{
|
|
|
|
# Some projects do not include a lock file, so you can pass one
|
2020-03-18 02:47:24 +00:00
|
|
|
lockFile ? null
|
|
|
|
# Generate shards.nix with `nix-shell -p crystal2nix --run crystal2nix` in the projects root
|
|
|
|
, shardsFile ? null
|
|
|
|
# We support different builders. To make things more straight forward, make it
|
|
|
|
# user selectable instead of trying to autodetect
|
|
|
|
, format ? "make"
|
|
|
|
, installManPages ? true
|
2019-08-26 16:22:55 +00:00
|
|
|
# Specify binaries to build in the form { foo.src = "src/foo.cr"; }
|
2021-09-10 20:24:51 +00:00
|
|
|
# The default `crystal build` options can be overridden with { foo.options = [ "--optionname" ]; }
|
2020-12-22 04:08:30 +00:00
|
|
|
, crystalBinaries ? { }
|
|
|
|
, ...
|
|
|
|
}@args:
|
2020-03-18 02:47:24 +00:00
|
|
|
|
|
|
|
assert (builtins.elem format [ "make" "crystal" "shards" ]);
|
2019-08-26 16:22:55 +00:00
|
|
|
let
|
2020-03-18 02:47:24 +00:00
|
|
|
mkDerivationArgs = builtins.removeAttrs args [
|
|
|
|
"format"
|
|
|
|
"installManPages"
|
|
|
|
"lockFile"
|
|
|
|
"shardsFile"
|
|
|
|
"crystalBinaries"
|
|
|
|
];
|
2019-08-26 16:22:55 +00:00
|
|
|
|
2020-12-22 04:08:30 +00:00
|
|
|
crystalLib = linkFarm "crystal-lib" (lib.mapAttrsToList
|
|
|
|
(name: value: {
|
|
|
|
inherit name;
|
|
|
|
path = fetchFromGitHub value;
|
|
|
|
})
|
|
|
|
(import shardsFile));
|
2019-08-26 16:22:55 +00:00
|
|
|
|
2021-09-10 20:24:51 +00:00
|
|
|
defaultOptions = [ "--release" "--progress" "--verbose" "--no-debug" ];
|
2019-08-26 16:22:55 +00:00
|
|
|
|
2020-03-18 02:47:24 +00:00
|
|
|
buildDirectly = shardsFile == null || crystalBinaries != { };
|
2019-08-26 16:22:55 +00:00
|
|
|
|
2020-12-22 04:08:30 +00:00
|
|
|
in
|
|
|
|
stdenv.mkDerivation (mkDerivationArgs // {
|
|
|
|
|
|
|
|
configurePhase = args.configurePhase or lib.concatStringsSep "\n"
|
|
|
|
(
|
|
|
|
[
|
|
|
|
"runHook preConfigure"
|
|
|
|
]
|
|
|
|
++ lib.optional (lockFile != null) "cp ${lockFile} ./shard.lock"
|
|
|
|
++ lib.optionals (shardsFile != null) [
|
|
|
|
"test -e lib || mkdir lib"
|
|
|
|
"for d in ${crystalLib}/*; do ln -s $d lib/; done"
|
|
|
|
"cp shard.lock lib/.shards.info"
|
|
|
|
]
|
|
|
|
++ [ "runHook postConfigure" ]
|
|
|
|
);
|
2019-08-26 16:22:55 +00:00
|
|
|
|
2020-03-18 02:47:24 +00:00
|
|
|
CRFLAGS = lib.concatStringsSep " " defaultOptions;
|
|
|
|
|
|
|
|
PREFIX = placeholder "out";
|
|
|
|
|
|
|
|
buildInputs = args.buildInputs or [ ] ++ [ crystal ]
|
|
|
|
++ lib.optional (format != "crystal") shards;
|
|
|
|
|
2021-01-19 06:50:56 +00:00
|
|
|
nativeBuildInputs = args.nativeBuildInputs or [ ] ++ [ git installShellFiles pkg-config which ];
|
2020-03-18 02:47:24 +00:00
|
|
|
|
|
|
|
buildPhase = args.buildPhase or (lib.concatStringsSep "\n" ([
|
|
|
|
"runHook preBuild"
|
|
|
|
] ++ lib.optional (format == "make")
|
2021-01-24 09:19:10 +00:00
|
|
|
"make \${buildTargets:-build} $makeFlags"
|
2020-12-22 04:08:30 +00:00
|
|
|
++ lib.optionals (format == "crystal") (lib.mapAttrsToList
|
|
|
|
(bin: attrs: ''
|
|
|
|
crystal ${lib.escapeShellArgs ([
|
|
|
|
"build"
|
|
|
|
"-o"
|
|
|
|
bin
|
|
|
|
(attrs.src or (throw "No source file for crystal binary ${bin} provided"))
|
|
|
|
] ++ (attrs.options or defaultOptions))}
|
|
|
|
'')
|
|
|
|
crystalBinaries)
|
2020-03-18 02:47:24 +00:00
|
|
|
++ lib.optional (format == "shards")
|
2020-12-22 04:08:30 +00:00
|
|
|
"shards build --local --production ${lib.concatStringsSep " " defaultOptions}"
|
2020-03-18 02:47:24 +00:00
|
|
|
++ [ "runHook postBuild" ]));
|
|
|
|
|
|
|
|
installPhase = args.installPhase or (lib.concatStringsSep "\n" ([
|
|
|
|
"runHook preInstall"
|
|
|
|
] ++ lib.optional (format == "make")
|
2021-01-24 09:19:10 +00:00
|
|
|
"make \${installTargets:-install} $installFlags"
|
2020-12-22 04:08:30 +00:00
|
|
|
++ lib.optionals (format == "crystal") (map
|
|
|
|
(bin: ''
|
2020-03-18 02:47:24 +00:00
|
|
|
install -Dm555 ${lib.escapeShellArgs [ bin "${placeholder "out"}/bin/${bin}" ]}
|
2020-12-22 04:08:30 +00:00
|
|
|
'')
|
|
|
|
(lib.attrNames crystalBinaries))
|
2020-03-18 02:47:24 +00:00
|
|
|
++ lib.optional (format == "shards")
|
2021-01-24 09:19:10 +00:00
|
|
|
"install -Dm555 bin/* -t $out/bin"
|
2020-03-18 02:47:24 +00:00
|
|
|
++ [
|
|
|
|
''
|
|
|
|
for f in README* *.md LICENSE; do
|
|
|
|
test -f $f && install -Dm444 $f -t $out/share/doc/${args.pname}
|
|
|
|
done
|
|
|
|
''
|
|
|
|
] ++ (lib.optional installManPages ''
|
2020-12-22 04:08:30 +00:00
|
|
|
if [ -d man ]; then
|
|
|
|
installManPage man/*.?
|
|
|
|
fi
|
2020-03-18 02:47:24 +00:00
|
|
|
'') ++ [
|
|
|
|
"runHook postInstall"
|
|
|
|
]));
|
|
|
|
|
|
|
|
doCheck = args.doCheck or true;
|
|
|
|
|
|
|
|
checkPhase = args.checkPhase or (lib.concatStringsSep "\n" ([
|
|
|
|
"runHook preCheck"
|
|
|
|
] ++ lib.optional (format == "make")
|
2021-01-24 09:19:10 +00:00
|
|
|
"make \${checkTarget:-test} $checkFlags"
|
2020-03-18 02:47:24 +00:00
|
|
|
++ lib.optional (format != "make")
|
2021-01-24 09:19:10 +00:00
|
|
|
"crystal \${checkTarget:-spec} $checkFlags"
|
2020-03-18 02:47:24 +00:00
|
|
|
++ [ "runHook postCheck" ]));
|
|
|
|
|
|
|
|
doInstallCheck = args.doInstallCheck or true;
|
2019-08-26 16:22:55 +00:00
|
|
|
|
2020-03-18 02:47:24 +00:00
|
|
|
installCheckPhase = args.installCheckPhase or ''
|
|
|
|
for f in $out/bin/*; do
|
2021-09-06 23:41:15 +00:00
|
|
|
$f --help > /dev/null
|
2020-03-18 02:47:24 +00:00
|
|
|
done
|
2019-08-26 16:22:55 +00:00
|
|
|
'';
|
|
|
|
|
2020-03-18 02:47:24 +00:00
|
|
|
meta = args.meta or { } // {
|
2019-08-26 16:22:55 +00:00
|
|
|
platforms = args.meta.platforms or crystal.meta.platforms;
|
|
|
|
};
|
|
|
|
})
|