Home

jsonnetpecker @1fefaff08881bf21a4cfa72385dbfdb5594335b6 - refs - log -
-
https://git.jolheiser.com/jsonnetpecker.git
Woodpecker config service for jsonnet
jsonnetpecker / flake.nix
- raw -
  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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
{
  description = "jsonnet config extension for woodpecker";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    gitpecker = {
      url = "git+https://git.jolheiser.com/gitpecker.git";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
  outputs =
    {
      self,
      nixpkgs,
      gitpecker,
      ...
    }:
    let
      systems = [
        "aarch64-darwin"
        "aarch64-linux"
        "x86_64-darwin"
        "x86_64-linux"
      ];
      forAllSystems = f: nixpkgs.lib.genAttrs systems f;
    in
    {
      packages = forAllSystems (
        system:
        let
          pkgs = import nixpkgs { inherit system; };
        in
        {
          default = self.packages.${system}.jsonnetpecker;
          jsonnetpecker = pkgs.buildGoModule rec {
            pname = "jsonnetpecker";
            version = self.rev or "dev";
            src = pkgs.nix-gitignore.gitignoreSource [ ] (
              builtins.path {
                name = pname;
                path = ./.;
              }
            );
            vendorHash = nixpkgs.lib.fileContents ./go.mod.sri;
            meta = {
              description = "jsonnet config extension for woodpecker";
              homepage = "https://git.jolheiser.com/jsonnetpecker";
              mainProgram = "jsonnetpecker";
            };
          };
        }
      );
      devShells = forAllSystems (
        system:
        let
          pkgs = import nixpkgs { inherit system; };
        in
        {
          default = pkgs.mkShell {
            nativeBuildInputs = with pkgs; [
              go
              gopls
              gofumpt
              woodpecker-server
            ];
          };
        }
      );
      nixosConfigurations.jsonnetpeckerVM = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          (
            { pkgs, ... }:
            {
              services.getty.autologinUser = "root";
              services.dex = {
                enable = true;
                settings = {
                  issuer = "http://localhost:5556/dex";
                  storage.type = "memory";
                  web.http = "0.0.0.0:5556";
                  connectors = [
                    {
                      type = "mockCallback";
                      id = "mock";
                      name = "Mock";
                    }
                  ];
                  oauth2.skipApprovalScreen = true;
                  staticClients = [
                    {
                      id = "woodpecker";
                      secret = "woodpecker-secret";
                      name = "Woodpecker";
                      redirectURIs = [ "http://localhost:8000/authorize" ];
                    }
                  ];
                };
              };
              services.woodpecker-server = {
                enable = true;
                package = pkgs.woodpecker-server.overrideAttrs (oldAttrs: {
                  doCheck = false;
                  patches = (oldAttrs.patches or [ ]) ++ [
                    ./jsonnet.patch
                    ./debug.patch
                  ];
                });
                environment = {
                  WOODPECKER_HOST = "http://0.0.0.0:8000";
                  WOODPECKER_OPEN = "true";
                  WOODPECKER_ADDON_FORGE = "${gitpecker.packages.x86_64-linux.gitpecker}/bin/gitpecker";
                  WOODPECKER_CONFIG_SERVICE_ENDPOINT = "http://localhost:8080";
                  WOODPECKER_EXTENSIONS_ALLOWED_HOSTS = "localhost";
                  WOODPECKER_LOG_LEVEL = "trace";
                  WOODPECKER_ADMIN = "kilgore@kilgore.trout";
                  WOODPECKER_AGENT_SECRET = "Testing123";
                  WOODPECKER_EXTRA_DEFAULT_PIPELINE_CONFIGS = ".woodpecker.jsonnet";
                  GITPECKER_REPOS = "/var/lib/forge/";
                  GITPECKER_URL = "/var/lib/forge/";
                  GITPECKER_PROVIDER = "http://localhost:5556/dex";
                  GITPECKER_CLIENT_ID = "woodpecker";
                  GITPECKER_CLIENT_SECRET = "woodpecker-secret";
                  GITPECKER_REDIRECT = "http://localhost:8000/authorize";
                };
              };
              systemd.services.woodpecker-server.after = [ "dex.service" ];
              services.woodpecker-agents.agents."007" = {
                enable = true;
                path = with pkgs; [
                  git
                  git-lfs
                  bash
                  coreutils
                  woodpecker-plugin-git
                ];
                environment = {
                  WOODPECKER_AGENT_SECRET = "Testing123";
                };
              };
              systemd.services.jsonnetpecker = {
                description = "jsonnetpecker config extension";
                wantedBy = [ "multi-user.target" ];
                after = [
                  "woodpecker-server.service"
                  "network.target"
                ];
                path = with pkgs; [ curl ];
                serviceConfig = {
                  Type = "simple";
                  RuntimeDirectory = "jsonnetpecker";
                  ExecStartPre = pkgs.writeShellScript "jsonnetpecker-fetch-pubkey" ''
                    until curl -sf http://localhost:8000/api/healthz > /dev/null 2>&1; do sleep 1; done
                    curl -sf "http://localhost:8000/api/signature/public-key" -o /run/jsonnetpecker/pubkey.pem
                  '';
                  ExecStart = "${self.packages.x86_64-linux.jsonnetpecker}/bin/jsonnetpecker --port 8080 --log-level debug --public-key-file /run/jsonnetpecker/pubkey.pem";
                };
              };
              virtualisation.vmVariant.virtualisation = {
                cores = 2;
                memorySize = 2048;
                graphics = false;
                forwardPorts = [
                  {
                    from = "host";
                    host.port = 8000;
                    guest.port = 8000;
                  }
                  {
                    from = "host";
                    host.port = 5556;
                    guest.port = 5556;
                  }
                ];
              };
              networking.firewall.enable = false;
              systemd.services."setup-vm" = {
                wantedBy = [ "multi-user.target" ];
                path = with pkgs; [
                  git
                ];
                serviceConfig = {
                  Type = "oneshot";
                  RemainAfterExit = true;
                  User = "root";
                  Group = "root";
                  ExecStart =
                    let
                      pipeline = pkgs.writeText "pipeline.jsonnet" ''
                        local step(name) = {
                          name: name,
                          image: "bash",
                          commands: ['echo "' + name + '"'],
                        };

                        {
                          // when: {event: "manual"},
                          steps: [step("honk"), step("bonk")],
                        }
                      '';
                    in
                    pkgs.writeShellScript "setup-vm-script" ''
                      git config --global user.name "NixUser"
                      git config --global user.email "nixuser@example.com"
                      git config --global init.defaultBranch main
                      git config --global push.autoSetupRemote true

                      mkdir -p /var/lib/forge
                      pushd /var/lib/forge
                      git init --bare test1.git
                      popd
                      git clone /var/lib/forge/test1.git
                      pushd test1
                      cp ${pipeline} .woodpecker.jsonnet
                      git add .
                      git commit -m "honk"
                      git push
                      popd
                    '';
                };
              };
              environment.systemPackages = with pkgs; [
                git
              ];
              system.stateVersion = "23.11";
            }
          )
        ];
      };
      apps = forAllSystems (
        system:
        let
          pkgs = import nixpkgs { inherit system; };
        in
        {
          vm = {
            type = "app";
            program = "${pkgs.writeShellScript "vm" ''
              nixos-rebuild build-vm --flake .#jsonnetpeckerVM
              QEMU_NET_OPTS="restrict=off" ./result/bin/run-nixos-vm
              rm nixos.qcow2
            ''}";
          };
        }
      );
    };
}