aboutsummaryrefslogtreecommitdiff
path: root/nixos/utils.nix
blob: 30110a482d347d475d7d985784aebe41b4d810e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{ 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 = ignoredFindPattern:
  baseTask.overrideAttrs (baseAttrs: {
    name = "${baseAttrs.name}-shellcheck";
    buildInputs = baseAttrs.buildInputs ++ [ pkgs.shellcheck ];
    buildPhase = ''
      find . -type f \( -name '*.sh' -and -regextype egrep ! -regex '${ignoredFindPattern}' \) -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 = 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
#     '';
#   });
# };