From 0ea63674b60f5d2b2088aa0fb6d599fcef96b783 Mon Sep 17 00:00:00 2001 From: Arnar Gauti Ingason Date: Fri, 22 Dec 2023 11:19:51 +0100 Subject: [PATCH] Add github action --- action.yml | 44 ++++++++++++++ modules/default.nix | 104 +------------------------------- modules/environment.nix | 128 ++++++++++++++++++++++++++++++++++++++++ modules/modules.nix | 1 + nixidy/nixidy | 26 ++++---- 5 files changed, 188 insertions(+), 115 deletions(-) create mode 100644 action.yml create mode 100644 modules/environment.nix diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..f0e4db3 --- /dev/null +++ b/action.yml @@ -0,0 +1,44 @@ +name: nixidy action +description: Generate manifest for a nixidy environment + +inputs: + environment: + description: Name of the environment to build + required: true + cwd: + description: Path to directory containing the checked out repository + default: '.' + +runs: + using: composite + steps: + - shell: bash + env: + FLAKE: github:${{github.action_repository}}/${{github.action_ref}} + DEST_DIR: ${{runner.temp}}/${{inputs.environment}} + run: | + TARGET_BRANCH=$(nix run '${{env.FLAKE}}#' -- info '${{inputs.cwd}}#${{inputs.environment}}' --json | jq -r .branch) + + if git -C "${{inputs.cwd}}" fetch origin "$TARGET_BRANCH"; then + git -C "${{inputs.cwd}}" worktree add --checkout "${{env.DEST_DIR}}" "$TARGET_BRANCH" + else + git -C "${{inputs.cwd}}" worktree add --orphan -b "$TARGET_BRANCH" "${{env.DEST_DIR}}" + fi + + RESULT=$(nix run '${{env.FLAKE}}#' -- build '${{inputs.cwd}}#${{inputs.environment}}' --print-out-paths --no-link) + + rsync --recursive --delete --exclude=.git -L "$RESULT/" "${{env.DEST_DIR}}" + + ls -al "${{env.DEST_DIR}}" + + echo "BRANCH=$TARGET_BRANCH" >> "$GITHUB_ENV" + + - uses: EndBug/add-and-commit@v9 + env: + DEST_DIR: ${{runner.temp}}/${{inputs.environment}} + with: + cwd: ${{env.DEST_DIR}} + default_author: github_actions + message: "chore(${{inputs.environment}}): promote to ${{inputs.environment}} ${{github.sha}}" + fetch: false + push: --set-upstream origin ${{env.BRANCH}} diff --git a/modules/default.nix b/modules/default.nix index d572ca3..381ad98 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -18,112 +18,10 @@ } // extraSpecialArgs; }; - - extras = pkgs.stdenv.mkDerivation { - name = "nixidy-extras"; - - phases = ["installPhase"]; - - installPhase = - '' - mkdir -p $out - '' - + ( - lib.concatStringsSep "\n" (lib.mapAttrsToList (n: f: '' - mkdir -p $out/$(dirname ${f.path}) - cat < $out/${f.path} - ${f.text} - EOF - '') - module.config.nixidy.extraFiles) - ); - }; - - apps = - lib.mapAttrs ( - n: v: { - name = n; - path = v.output.path; - resources = lib.flatten ( - lib.mapAttrsToList ( - group: groupData: - lib.mapAttrsToList ( - kind: kindData: - lib.mapAttrsToList ( - res: resData: - resData - // { - apiVersion = group; - kind = kind; - metadata = {name = res;} // (resData.metadata or {}); - } - ) - kindData - ) - groupData - ) - v.resources - ); - } - ) - module.config.applications; - - mkApp = app: let - resources = - map ( - res: rec { - filename = "${res.kind}-${builtins.replaceStrings ["."] ["-"] res.metadata.name}.yaml"; - manifest = let - resource = builtins.toJSON res; - in - pkgs.stdenv.mkDerivation { - inherit resource; - - name = "nixidy-app-${app.name}-${filename}"; - - passAsFile = ["resource"]; - - phases = ["installPhase"]; - - installPhase = '' - cat $resourcePath | ${pkgs.yq-go}/bin/yq -P > $out - ''; - }; - } - ) - app.resources; - in - pkgs.stdenv.mkDerivation { - name = "nixidy-app-${app.name}"; - - phases = ["installPhase"]; - - installPhase = - '' - mkdir -p $out - '' - + (lib.concatStringsSep "\n" (map (res: '' - ln -s ${res.manifest} $out/${res.filename} - '') - resources)); - }; - - mkStage = apps: let - appsJoined = pkgs.linkFarm "nixidy-stage-apps-joined" (lib.mapAttrsToList (_: app: { - name = app.path; - path = mkApp app; - }) - apps); - in - pkgs.symlinkJoin { - name = "nixidy-stage"; - paths = [appsJoined extras]; - }; in { - targetBranch = module.config.nixidy.target.branch; meta = { repository = module.config.nixidy.target.repository; branch = module.config.nixidy.target.branch; }; - result = mkStage apps; + environmentPackage = module.config.nixidy.environmentPackage; } diff --git a/modules/environment.nix b/modules/environment.nix new file mode 100644 index 0000000..f77d9f5 --- /dev/null +++ b/modules/environment.nix @@ -0,0 +1,128 @@ +{ + lib, + config, + pkgs, + ... +}: let + envName = lib.replaceStrings ["/"] ["-"] config.nixidy.target.branch; + + apps = + lib.mapAttrs ( + n: v: { + name = n; + path = v.output.path; + resources = lib.flatten ( + lib.mapAttrsToList ( + group: groupData: + lib.mapAttrsToList ( + kind: kindData: + lib.mapAttrsToList ( + res: resData: + resData + // { + apiVersion = group; + kind = kind; + metadata = {name = res;} // (resData.metadata or {}); + } + ) + kindData + ) + groupData + ) + v.resources + ); + } + ) + config.applications; + + mkApp = app: let + resources = + map ( + res: rec { + filename = "${res.kind}-${builtins.replaceStrings ["."] ["-"] res.metadata.name}.yaml"; + manifest = let + resource = builtins.toJSON res; + in + pkgs.stdenv.mkDerivation { + inherit resource; + + name = "nixidy-app-${app.name}-${filename}"; + + passAsFile = ["resource"]; + + phases = ["installPhase"]; + + installPhase = '' + cat $resourcePath | ${pkgs.yq-go}/bin/yq -P > $out + ''; + }; + } + ) + app.resources; + in + pkgs.stdenv.mkDerivation { + name = "nixidy-app-${app.name}"; + + phases = ["installPhase"]; + + installPhase = + '' + mkdir -p $out + '' + + (lib.concatStringsSep "\n" (map (res: '' + ln -s ${res.manifest} $out/${res.filename} + '') + resources)); + }; +in { + options = with lib; { + nixidy.extrasPackage = mkOption { + type = types.package; + internal = true; + description = "The package containing all the extra files for an environment."; + }; + + nixidy.environmentPackage = mkOption { + type = types.package; + internal = true; + description = "The package containing all the applications for an environment."; + }; + }; + + config = { + # Build all extra files into its own package + nixidy.extrasPackage = pkgs.stdenv.mkDerivation { + name = "nixidy-extras-${envName}"; + + phases = ["installPhase"]; + + installPhase = + '' + mkdir -p $out + '' + + (lib.concatStringsSep "\n" (lib.mapAttrsToList (_: file: '' + mkdir -p $out/$(dirname ${file.path}) + cat < "$out/${file.path}" + ${file.text} + EOF + '') + config.nixidy.extraFiles)); + }; + + # Build final environment into a package + nixidy.environmentPackage = let + joined = pkgs.linkFarm "nixidy-apps-joined-${envName}" (lib.mapAttrsToList (_: app: { + name = app.path; + path = mkApp app; + }) + apps); + in + pkgs.symlinkJoin { + name = "nixidy-environment-${envName}"; + paths = [ + joined + config.nixidy.extrasPackage + ]; + }; + }; +} diff --git a/modules/modules.nix b/modules/modules.nix index 696cfa8..dc5947a 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -1,4 +1,5 @@ [ ./applications.nix ./nixidy.nix + ./environment.nix ] diff --git a/nixidy/nixidy b/nixidy/nixidy index 86697ef..6346c7b 100644 --- a/nixidy/nixidy +++ b/nixidy/nixidy @@ -47,7 +47,7 @@ function doBuild() { exit 1 fi - nix build "${FLAKE_ROOT}#nixidyEnvs.${NIX_SYSTEM}.${FLAKE_ENV}.result" "${BUILD_PARAMS[@]}" + nix build "${FLAKE_ROOT}#nixidyEnvs.${NIX_SYSTEM}.${FLAKE_ENV}.environmentPackage" "${BUILD_PARAMS[@]}" } function doHelp() { @@ -55,22 +55,21 @@ function doHelp() { echo echo "Options" echo - echo " --no-link Don't create a result symlink (only used in build)." - echo " --out-link PATH Create a custom result symlink (only used in build)." - echo " --json Output info in JSON format (only used in info)." - echo " -h Print this help" + echo " --no-link Don't create a result symlink (only used in build)." + echo " --out-link PATH Create a custom result symlink (only used in build)." + echo " --print-out-paths Print the resulting output paths (only used in build)." + echo " --json Output info in JSON format (only used in info)." + echo " -h Print this help" echo echo "Commands" echo - echo " help Print this help." + echo " help Print this help." echo - echo " info FLAKE_URI" - echo " Get info about environment." - echo " Example: .#prod" + echo " info FLAKE_URI Get info about environment." + echo " Example: .#prod" echo - echo " build FLAKE_URI" - echo " Build nixidy environment from flake URI." - echo " Example: .#prod" + echo " build FLAKE_URI Build nixidy environment from flake URI." + echo " Example: .#prod" } COMMAND="" @@ -89,6 +88,9 @@ while [[ $# -gt 0 ]]; do --no-link) BUILD_PARAMS+=("--no-link") ;; + --print-out-paths) + BUILD_PARAMS+=("--print-out-paths") + ;; --out-link) BUILD_PARAMS+=("--out-link" "$1") shift