veloren/nix/utils.nix
Ludvig Böklin 3f498522db flake-compat (unify flake and non-flake build process/environment)
The only user-facing changes is the process for version pinning, which will be
slightly less convenient without access to the `nix flake` feature. This is
justified by it rarely being necessary, for the benefit of relying on an
upcoming built-in nix feature rather than a non-standard third-party tool.

- deprecate `niv` for version pinning
- legacy nix now uses the flake via `flake-compat`
  - remove `sources.{nix,json}` (versions now pinned in `flake.lock`)
  - move contents of `nix/default.nix` to `nix/veloren.nix`
  - make `nix-build nix/default.nix` produce the same output as `flake build`
  - move contents of `nix/shell.nix` into `nix/devShell.nix`
  - make `nix-shell nix/shell.nix` produce the same environment as `flake develop`
- Move `tag` parameter into `sourceInfo`
- Tidy up and autoformat with `nixpkgs-fmt`
- update README to reflect new usage
- revert input versions to match those previously specified in nix/sources.json
2020-12-11 12:00:04 +00:00

76 lines
2.5 KiB
Nix

{ pkgs }: {
isGitLfsSetup = checkFile:
let
gitLfsCheckOutput =
builtins.readFile (pkgs.runCommand "gitLfsCheck" { } ''
([ "$(${pkgs.file}/bin/file --mime-type ${checkFile})" = "${checkFile}: image/png" ] && printf "0" || printf "1") > $out
'');
in
if gitLfsCheckOutput == "0" then
true
else
abort ''
Git Large File Storage (`git-lfs`) has not been set up correctly.
Most common reasons:
- `git-lfs` was not installed before cloning this repository.
- This repository was not cloned from the primary GitLab mirror.
- The GitHub mirror does not support LFS.
See the book at https://book.veloren.net/ for details.
'';
# Format number of seconds in the Unix epoch as %Y-%m-%d-%H:%M.
dateTimeFormat = t:
let
rem = x: y: x - x / y * y;
days = t / 86400;
secondsInDay = rem t 86400;
hours = secondsInDay / 3600;
minutes = (rem secondsInDay 3600) / 60;
seconds = rem t 60;
# Courtesy of https://stackoverflow.com/a/32158604.
z = days + 719468;
era = (if z >= 0 then z else z - 146096) / 146097;
doe = z - era * 146097;
yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
y = yoe + era * 400;
doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
mp = (5 * doy + 2) / 153;
d = doy - (153 * mp + 2) / 5 + 1;
m = mp + (if mp < 10 then 3 else -9);
y' = y + (if m <= 2 then 1 else 0);
pad = s: if builtins.stringLength s < 2 then "0" + s else s;
in
"${toString y'}-${pad (toString m)}-${pad (toString d)}-${pad (toString hours)}:${pad (toString minutes)}";
getGitInfo = dotGitPath:
let
makeGitCommand = subcommands: name:
builtins.readFile (pkgs.runCommand name { } ''
cd ${
# Only copy the `.git` directory to nix store, anything else is a waste.
builtins.path {
path = ../.git;
# Nix store path names don't accept names that start with a dot.
name = "dotgit-dir";
}
}
(${pkgs.git}/bin/git ${subcommands}) > $out
'');
in
{
gitHash = makeGitCommand
"log -n 1 --pretty=format:%h/%cd --date=format:%Y-%m-%d-%H:%M --abbrev=8"
"getGitHash";
gitTag =
# If the git command errors out we feed an empty string
makeGitCommand "describe --exact-match --tags HEAD || printf ''"
"getGitTag";
};
}