aboutsummaryrefslogtreecommitdiff
path: root/nixos/utils.nix
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2019-05-30 21:51:13 -0300
committerEuAndreh <eu@euandre.org>2019-05-30 21:51:13 -0300
commitf77fc421dee47ddc45106891b155ebd1693fe9ba (patch)
tree68b37e41f92183e2ed34128d3277063aa98a9056 /nixos/utils.nix
parentAnnotate files with FIXME (diff)
downloaddotfiles-f77fc421dee47ddc45106891b155ebd1693fe9ba.tar.gz
dotfiles-f77fc421dee47ddc45106891b155ebd1693fe9ba.tar.xz
Format all Nix code with nixfmt
Diffstat (limited to 'nixos/utils.nix')
-rw-r--r--nixos/utils.nix118
1 files changed, 118 insertions, 0 deletions
diff --git a/nixos/utils.nix b/nixos/utils.nix
new file mode 100644
index 0000000..f809dd9
--- /dev/null
+++ b/nixos/utils.nix
@@ -0,0 +1,118 @@
+{ pkgs, rootSrc ? pkgs.nix-gitignore.gitignoreSource [ ] ./., 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 = rootSrc;
+ buildInputs = [ ];
+ phases = "unpackPhase buildPhase";
+ buildPhase = ''
+ echo "ERROR: base task buildPhase not overriden."
+ exit 1
+ '';
+ };
+ shellcheck = baseTask.overrideAttrs (baseAttrs: {
+ name = "${baseAttrs.name}-shellcheck";
+ buildInputs = baseAttrs.buildInputs ++ [ pkgs.shellcheck ];
+ buildPhase = ''
+ export SHELLCHECK_OPTS="-e SC1090 -e SC1091 -e SC2139"
+ ignored='(encrypted|os-installation.sh|notmuch-post.sh)'
+ find . -type f -name '*.sh' | grep -E -v $ignored | xargs 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 = baseTask.overrideAttrs (baseAttrs: {
+ name = "${baseAttrs.name}-fixme";
+ buildInputs = baseAttrs.buildInputs ++ [ pkgs.ag ];
+ buildPhase = ''
+ ag FIXME --ignore default.nix || {
+ touch $out
+ exit 0
+ }
+ echo "^^^^^^^^^^^^^^^^^"
+ echo " Found dangling FIXME markers on the project xp"
+ exit 1
+ '';
+ });
+
+ 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
+ '';
+ });
+}
+
+# subtasks = rec {
+# shellcheck = baseTask.overrideAttrs(baseAttrs: {
+# name = "${baseAttrs.name}-shellcheck";
+# buildInputs = baseAttrs.buildInputs ++ [ pkgs.shellcheck ];
+# buildPhase = ''
+# export SHELLCHECK_OPTS="-e SC1090 -e SC1091 -e SC2139"
+# ignored='(encrypted|os-installation.sh|notmuch-post.sh)'
+# find . -type f -name '*.sh' | grep -E -v $ignored | xargs shellcheck
+# touch $out
+# '';
+# });
+# fixme = baseTask.overrideAttrs(baseAttrs: {
+# name = "${baseAttrs.name}-fixme";
+# buildInputs = baseAttrs.buildInputs ++ [ ag ];
+# buildPhase = ''
+# ag FIXME --ignore default.nix || {
+# touch $out
+# }
+# '';
+# });
+# uniqueFeeds = baseTask.overrideAttrs (baseAttrs: {
+# name = "${baseAttrs.name}-unique-feeds";
+# buildPhase = ''
+# OUT="$(uniq -D <(sort ./newsboat/urls))"
+# [[ $OUT = "" ]] || {
+# echo "Duplicate subscriptions found in ./newsboat/urls:"
+# echo "$OUT"
+# exit 1
+# }
+# touch $out
+# '';
+# });
+# formatNix = baseTask.overrideAttrs (baseAttrs: {
+# name = "${baseAttrs.name}-nixfmt";
+# buildInputs = baseAttrs.buildInputs ++ [nixfmt];
+# buildPhase = ''
+# diff <(nixfmt < default.nix) default.nix || {
+# echo "The default.nix is unformatted. To fix it, run:"
+# echo " nixfmt default.nix"
+# exit 1
+# }
+# touch $out
+# '';
+# });
+# };