aboutsummaryrefslogtreecommitdiff
path: root/utils.nix
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2019-06-01 00:32:20 -0300
committerEuAndreh <eu@euandre.org>2019-06-01 00:41:29 -0300
commit39112c6771409d5b2870052308980359a36d3159 (patch)
tree31574f66e6b847c71baf2b6c70cb2c8b9928b0a0 /utils.nix
parentUse pinned version of hakyll (diff)
downloadeuandre.org-39112c6771409d5b2870052308980359a36d3159.tar.gz
euandre.org-39112c6771409d5b2870052308980359a36d3159.tar.xz
Move test code out of default.nix
- use common utils.nix code for generic test derivation; - move spell checking code into standalone Bash file.
Diffstat (limited to '')
-rw-r--r--utils.nix101
1 files changed, 101 insertions, 0 deletions
diff --git a/utils.nix b/utils.nix
new file mode 100644
index 0000000..b41e9f5
--- /dev/null
+++ b/utils.nix
@@ -0,0 +1,101 @@
+{ pkgs, src, baseName }:
+let
+ nixfmt = import (builtins.fetchTarball
+ "https://github.com/serokell/nixfmt/archive/master.tar.gz") { };
+in rec {
+ baseTask = pkgs.stdenv.mkDerivation {
+ name = "${baseName}-task";
+ src = src;
+ buildInputs = [ ];
+ phases = "unpackPhase buildPhase";
+ buildPhase = ''
+ echo "ERROR: base task buildPhase not overriden."
+ exit 1
+ '';
+ };
+ shellcheck = ignoredFindPattern:
+ baseTask.overrideAttrs (baseAttrs: rec {
+ name = "${baseAttrs.name}-shellcheck";
+ buildInputs = baseAttrs.buildInputs ++ [ pkgs.shellcheck ];
+ ignoredPattern = if ignoredFindPattern == null then "" else ignoredFindPattern;
+ buildPhase = ''
+ find . -type f \( -name '*.sh' -and -regextype egrep ! -regex '${ignoredPattern}' \) -print0 | xargs -0 shellcheck
+ touch $out
+ '';
+ });
+ formatNix = baseTask.overrideAttrs (baseAttrs: {
+ name = "${baseAttrs.name}-nixfmt";
+ buildInputs = baseAttrs.buildInputs ++ [ nixfmt ];
+ buildPhase = ''
+ format() {
+ nix_file="$1"
+ diff <(nixfmt < "$nix_file") "$nix_file" || {
+ echo "The $nix_file is unformatted. To fix it, run:"
+ echo " nixfmt $nix_file"
+ exit 1
+ }
+ }
+ export -f format
+ find . -type f -name '*.nix' -print0 | xargs -0 -I{} bash -c "format {}" \;
+ touch $out
+ '';
+ });
+ fixme = ignoredFiles:
+ baseTask.overrideAttrs (baseAttrs: rec {
+ name = "${baseAttrs.name}-fixme";
+ buildInputs = baseAttrs.buildInputs ++ [ pkgs.ag ];
+ ignoredPattern = pkgs.lib.fold (a: b: " --ignore ${a} ${b}") ""
+ (if ignoredFiles == null then [
+ "default.nix"
+ "TODOs.org"
+ ] else
+ ignoredFiles);
+ buildPhase = ''
+ ag FIXME ${ignoredPattern} || {
+ touch $out
+ exit 0
+ }
+ echo " Found dangling FIXME markers on the project."
+ echo " You should write them down properly on TODOs.org."
+ exit 1
+ '';
+ });
+ orgMkDocs = title:
+ baseTask.overrideAttrs (baseAttrs: {
+ name = "${baseAttrs.name}-docs";
+ buildInputs = [ pkgs.pandoc pkgs.mkdocs ];
+ buildPhase = ''
+ # Convert from org-mode to markdown with pandoc
+ find docs/ -type f -name '*.org' -print0 | xargs -0 -I{} pandoc -o {}.md {}.org
+
+ # Give the generated markdown files to MkDocs
+ mkdocs build
+
+ # Build remaining one-off files
+ pandoc README.org -o site/index.html --css docs/README.css --to=html5 --self-contained --metadata title="${title}"
+
+ mv site/ $out/
+ '';
+ });
+ test = testDerivations:
+ baseTask.overrideAttrs (baseAttrs: {
+ name = "${baseAttrs.name}-test";
+ buildPhase = ''
+ echo "Ran tests for:"
+ for d in ${builtins.toString testDerivations}; do
+ echo " $d"
+ done
+ echo "All tests passed!"
+ touch $out
+ '';
+ });
+ publishScript = docsDerivation:
+ pkgs.writeShellScriptBin "publish.sh" ''
+ set -euo pipefail
+ OUT_DOCS="${docsDerivation}"
+ ${pkgs.rsync}/bin/rsync -avzP \
+ --rsh="ssh -o StrictHostKeyChecking=no" \
+ "$OUT_DOCS/" \
+ "$SERVER_URL:$DOCS_SERVER_PATH"
+ '';
+}