aboutsummaryrefslogtreecommitdiff
path: root/default.nix
blob: 68604f91c0f69c9ad9f19c21eeb0ffaa76a9773b (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
let
  pkgsUnstable = import <nixpkgs> { };
  pkgsPinned = import (pkgsUnstable.fetchzip {
    url = "https://github.com/NixOS/nixpkgs/archive/18.03.zip";
    sha256 = "0hk4y2vkgm1qadpsm4b0q1vxq889jhxzjx3ragybrlwwg54mzp4f";
  }) { };
  pkgs = pkgsUnstable;

  rootSrc = pkgs.nix-gitignore.gitignoreSource [ ] ./.;
  nixfmt = import (builtins.fetchTarball
  "https://github.com/serokell/nixfmt/archive/master.tar.gz") { };

  baseTask = pkgs.stdenv.mkDerivation {
    name = "vps-task";
    src = rootSrc;
    phases = "unpackPhase buildPhase";
    buildInputs = [ ];
    buildPhase = ''
      echo "ERROR: base task buildPhase not overriden."
      exit 1
    '';
  };
in with pkgs;
with pkgs.stdenv; rec {
  subtasks = rec {
    shellCheck = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-shellcheck";
      buildInputs = baseAttrs.buildInputs ++ [ shellcheck ];
      buildPhase = ''
        find . -type f -name '*.sh' | xargs shellcheck
        touch $out
      '';
    });
    fixme = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-fixme";
      buildInputs = baseAttrs.buildInputs ++ [ ag ];
      buildPhase = ''
        ag FIXME --ignore default.nix || {
          touch $out
          exit 0
        }
        echo "^^^^^^^^^^^^^^^^^"
        echo "  Found dangling FIXME markers on the project xp"
        exit 1
      '';
    });
    formatTerraform = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-format-terraform";
      buildInputs = baseAttrs.buildInputs ++ [ terraform ];
      buildPhase = ''
        terraform fmt -check=true -diff=true || {
          echo "Terraform files are unformatted. To fix it, run:"
          echo "  terraform fmt"
          exit 1
        }
        touch $out
      '';
    });
    dockerComposeLint = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-docker-compose-lint";
      buildInputs = baseAttrs.buildInputs ++ [ docker-compose ];
      buildPhase = ''
        source .envrc
        docker-compose config &> /dev/null || {
          echo "Invalid docker-compose.yml file."
          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
      '';
    });
  };
  test = baseTask.overrideAttrs (baseAttrs: rec {
    name = "${baseAttrs.name}-test";
    buildInputs = [
      subtasks.shellCheck
      subtasks.fixme
      subtasks.formatTerraform
      subtasks.dockerComposeLint
      subtasks.formatNix
    ];
    buildPhase = ''
      echo "Ran tests for:"
      for d in ${builtins.toString buildInputs}; do
        echo "  $d"
      done
      echo "All tests passed!"
      touch $out
    '';
  });
  # Used in .build.yml to run Bash scripts
  shell = mkShell rec {
    name = "vps-shell";
    buildInputs =
    [ gitMinimal git-crypt gettext terraform-providers.digitalocean terraform ];
  };
}