From 03e9a3652f79f874bb031156eef2bb88285ee5b7 Mon Sep 17 00:00:00 2001 From: ahuston-0 Date: Mon, 15 Jul 2024 19:42:22 -0400 Subject: [PATCH] add hydra, update ruff, format files Signed-off-by: ahuston-0 --- checks.nix | 2 +- flake.nix | 29 +++++++++++++--- hydra/jobs.nix | 19 +++++++++++ hydra/jobsets.nix | 87 +++++++++++++++++++++++++++++++++++++++++++++++ hydra/spec.json | 35 +++++++++++++++++++ poetry.lock | 39 ++++++++++----------- pyproject.toml | 2 +- shell.nix | 70 ++++++++++++++++++-------------------- treefmt.toml | 22 ++++++++++++ 9 files changed, 242 insertions(+), 63 deletions(-) create mode 100644 hydra/jobs.nix create mode 100644 hydra/jobsets.nix create mode 100644 hydra/spec.json create mode 100644 treefmt.toml diff --git a/checks.nix b/checks.nix index 1da2fc7..2bc11b6 100644 --- a/checks.nix +++ b/checks.nix @@ -4,7 +4,7 @@ formatter, ... }: - { +{ pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run { src = ./.; hooks = { diff --git a/flake.nix b/flake.nix index e9245d4..0fee803 100644 --- a/flake.nix +++ b/flake.nix @@ -18,8 +18,16 @@ }; }; - outputs = { self, nixpkgs, flake-utils, poetry2nix,pre-commit-hooks }@inputs: - flake-utils.lib.eachDefaultSystem (system: + outputs = + { + self, + nixpkgs, + flake-utils, + poetry2nix, + ... + }@inputs: + flake-utils.lib.eachDefaultSystem ( + system: let # see https://github.com/nix-community/poetry2nix/tree/master#api for more functions and examples. pkgs = nixpkgs.legacyPackages.${system}; @@ -33,7 +41,18 @@ formatter = pkgs.nixfmt-rfc-style; - devShells = import ./shell.nix {inherit self inputs system checks;}; - checks = import ./checks.nix {inherit inputs system formatter;}; - }); + devShells = import ./shell.nix { + inherit + self + inputs + system + checks + ; + }; + checks = import ./checks.nix { inherit inputs system formatter; }; + } + ) + // { + hydraJobs = import ./hydra/jobs.nix { inherit (self) inputs outputs; }; + }; } diff --git a/hydra/jobs.nix b/hydra/jobs.nix new file mode 100644 index 0000000..2e30f2f --- /dev/null +++ b/hydra/jobs.nix @@ -0,0 +1,19 @@ +{ inputs, outputs }: +let + inherit (inputs.nixpkgs) lib; + + notBroken = pkg: !(pkg.meta.broken or false); + isDistributable = pkg: (pkg.meta.license or { redistributable = true; }).redistributable; + hasPlatform = sys: pkg: lib.elem sys (pkg.meta.platforms or [ sys ]); + filterValidPkgs = + sys: pkgs: + lib.filterAttrs ( + _: pkg: lib.isDerivation pkg && hasPlatform sys pkg && notBroken pkg && isDistributable pkg + ) pkgs; +in +# getCfg = _: cfg: cfg.config.system.build.toplevel; +{ + # hosts = lib.mapAttrs getCfg outputs.nixosConfigurations; + inherit (outputs) devShells checks formatter; + packages = lib.mapAttrs filterValidPkgs outputs.packages; +} diff --git a/hydra/jobsets.nix b/hydra/jobsets.nix new file mode 100644 index 0000000..03654ce --- /dev/null +++ b/hydra/jobsets.nix @@ -0,0 +1,87 @@ +{ pulls, branches, ... }: +let + # create the json spec for the jobset + makeSpec = + contents: + builtins.derivation { + name = "spec.json"; + system = "x86_64-linux"; + preferLocalBuild = true; + allowSubstitutes = false; + builder = "/bin/sh"; + args = [ + (builtins.toFile "builder.sh" '' + echo "$contents" > $out + '') + ]; + contents = builtins.toJSON contents; + }; + + prs = readJSONFile pulls; + refs = readJSONFile branches; + repo = "ahuston-0/canvas_grit_automation"; + + # template for creating a job + makeJob = + { + schedulingshares ? 10, + keepnr ? 3, + description, + flake, + }: + { + inherit + description + flake + schedulingshares + keepnr + ; + enabled = 1; + type = 1; + hidden = false; + checkinterval = 300; # every 6 months + enableemail = false; + emailoverride = ""; + }; + + # Create a hydra job for a branch + jobOfRef = + name: + { ref, ... }: + if (builtins.match "^refs/heads/(.*)$" ref) == null then + null + else + { + name = builtins.replaceStrings [ "/" ] [ "-" ] "branch-${name}"; + value = makeJob { + description = "Branch ${name}"; + flake = "git+ssh://git@github.com/${repo}?ref=${ref}"; + }; + }; + + # Create a hydra job for a PR + jobOfPR = id: info: { + name = "pr-${id}"; + value = makeJob { + description = "PR ${id}: ${info.title}"; + flake = "git+ssh://git@github.com/${info.head.repo.full_name}?ref=${info.head.ref}"; + }; + }; + + # some utility functions + # converts json to name/value dicts + attrsToList = l: builtins.attrValues (builtins.mapAttrs (name: value: { inherit name value; }) l); + # wrapper function for reading json from file + readJSONFile = f: builtins.fromJSON (builtins.readFile f); + # remove null values from a set, in-case of branches that don't exist + mapFilter = f: l: builtins.filter (x: x != null) (map f l); + + # Create job set from PRs and branches + jobs = makeSpec ( + builtins.listToAttrs (map ({ name, value }: jobOfPR name value) (attrsToList prs)) + // builtins.listToAttrs (mapFilter ({ name, value }: jobOfRef name value) (attrsToList refs)) + ); +in +{ + jobsets = jobs; +} diff --git a/hydra/spec.json b/hydra/spec.json new file mode 100644 index 0000000..be4a62b --- /dev/null +++ b/hydra/spec.json @@ -0,0 +1,35 @@ +{ + "enabled": 1, + "hidden": false, + "description": "Canvas grit automation", + "nixexprinput": "nixexpr", + "nixexprpath": "hydra/jobsets.nix", + "checkinterval": 60, + "schedulingshares": 100, + "enableemail": false, + "emailoverride": "", + "keepnr": 3, + "type": 0, + "inputs": { + "nixexpr": { + "value": "https://github.com/ahuston-0/canvas_grit_automation master", + "type": "git", + "emailresponsible": false + }, + "nixpkgs": { + "value": "https://github.com/NixOS/nixpkgs nixos-unstable-small", + "type": "git", + "emailresponsible": false + }, + "pulls": { + "type": "githubpulls", + "value": "ahuston-0 canvas_grit_automation", + "emailresponsible": false + }, + "branches": { + "type": "github_refs", + "value": "ahuston-0 canvas_grit_automation heads -", + "emailresponsible": false + } + } +} diff --git a/poetry.lock b/poetry.lock index 3ba2a59..d16b581 100644 --- a/poetry.lock +++ b/poetry.lock @@ -73,28 +73,29 @@ test = ["objgraph", "psutil"] [[package]] name = "ruff" -version = "0.4.10" +version = "0.5.1" description = "An extremely fast Python linter and code formatter, written in Rust." optional = false python-versions = ">=3.7" files = [ - {file = "ruff-0.4.10-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:5c2c4d0859305ac5a16310eec40e4e9a9dec5dcdfbe92697acd99624e8638dac"}, - {file = "ruff-0.4.10-py3-none-macosx_11_0_arm64.whl", hash = "sha256:a79489607d1495685cdd911a323a35871abfb7a95d4f98fc6f85e799227ac46e"}, - {file = "ruff-0.4.10-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b1dd1681dfa90a41b8376a61af05cc4dc5ff32c8f14f5fe20dba9ff5deb80cd6"}, - {file = "ruff-0.4.10-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:c75c53bb79d71310dc79fb69eb4902fba804a81f374bc86a9b117a8d077a1784"}, - {file = "ruff-0.4.10-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:18238c80ee3d9100d3535d8eb15a59c4a0753b45cc55f8bf38f38d6a597b9739"}, - {file = "ruff-0.4.10-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:d8f71885bce242da344989cae08e263de29752f094233f932d4f5cfb4ef36a81"}, - {file = "ruff-0.4.10-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:330421543bd3222cdfec481e8ff3460e8702ed1e58b494cf9d9e4bf90db52b9d"}, - {file = "ruff-0.4.10-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:9e9b6fb3a37b772628415b00c4fc892f97954275394ed611056a4b8a2631365e"}, - {file = "ruff-0.4.10-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0f54c481b39a762d48f64d97351048e842861c6662d63ec599f67d515cb417f6"}, - {file = "ruff-0.4.10-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:67fe086b433b965c22de0b4259ddfe6fa541c95bf418499bedb9ad5fb8d1c631"}, - {file = "ruff-0.4.10-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:acfaaab59543382085f9eb51f8e87bac26bf96b164839955f244d07125a982ef"}, - {file = "ruff-0.4.10-py3-none-musllinux_1_2_i686.whl", hash = "sha256:3cea07079962b2941244191569cf3a05541477286f5cafea638cd3aa94b56815"}, - {file = "ruff-0.4.10-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:338a64ef0748f8c3a80d7f05785930f7965d71ca260904a9321d13be24b79695"}, - {file = "ruff-0.4.10-py3-none-win32.whl", hash = "sha256:ffe3cd2f89cb54561c62e5fa20e8f182c0a444934bf430515a4b422f1ab7b7ca"}, - {file = "ruff-0.4.10-py3-none-win_amd64.whl", hash = "sha256:67f67cef43c55ffc8cc59e8e0b97e9e60b4837c8f21e8ab5ffd5d66e196e25f7"}, - {file = "ruff-0.4.10-py3-none-win_arm64.whl", hash = "sha256:dd1fcee327c20addac7916ca4e2653fbbf2e8388d8a6477ce5b4e986b68ae6c0"}, - {file = "ruff-0.4.10.tar.gz", hash = "sha256:3aa4f2bc388a30d346c56524f7cacca85945ba124945fe489952aadb6b5cd804"}, + {file = "ruff-0.5.1-py3-none-linux_armv6l.whl", hash = "sha256:6ecf968fcf94d942d42b700af18ede94b07521bd188aaf2cd7bc898dd8cb63b6"}, + {file = "ruff-0.5.1-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:204fb0a472f00f2e6280a7c8c7c066e11e20e23a37557d63045bf27a616ba61c"}, + {file = "ruff-0.5.1-py3-none-macosx_11_0_arm64.whl", hash = "sha256:d235968460e8758d1e1297e1de59a38d94102f60cafb4d5382033c324404ee9d"}, + {file = "ruff-0.5.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:38beace10b8d5f9b6bdc91619310af6d63dd2019f3fb2d17a2da26360d7962fa"}, + {file = "ruff-0.5.1-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:5e478d2f09cf06add143cf8c4540ef77b6599191e0c50ed976582f06e588c994"}, + {file = "ruff-0.5.1-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:f0368d765eec8247b8550251c49ebb20554cc4e812f383ff9f5bf0d5d94190b0"}, + {file = "ruff-0.5.1-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:3a9a9a1b582e37669b0138b7c1d9d60b9edac880b80eb2baba6d0e566bdeca4d"}, + {file = "ruff-0.5.1-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:bdd9f723e16003623423affabcc0a807a66552ee6a29f90eddad87a40c750b78"}, + {file = "ruff-0.5.1-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:be9fd62c1e99539da05fcdc1e90d20f74aec1b7a1613463ed77870057cd6bd96"}, + {file = "ruff-0.5.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e216fc75a80ea1fbd96af94a6233d90190d5b65cc3d5dfacf2bd48c3e067d3e1"}, + {file = "ruff-0.5.1-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:c4c2112e9883a40967827d5c24803525145e7dab315497fae149764979ac7929"}, + {file = "ruff-0.5.1-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:dfaf11c8a116394da3b65cd4b36de30d8552fa45b8119b9ef5ca6638ab964fa3"}, + {file = "ruff-0.5.1-py3-none-musllinux_1_2_i686.whl", hash = "sha256:d7ceb9b2fe700ee09a0c6b192c5ef03c56eb82a0514218d8ff700f6ade004108"}, + {file = "ruff-0.5.1-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:bac6288e82f6296f82ed5285f597713acb2a6ae26618ffc6b429c597b392535c"}, + {file = "ruff-0.5.1-py3-none-win32.whl", hash = "sha256:5c441d9c24ec09e1cb190a04535c5379b36b73c4bc20aa180c54812c27d1cca4"}, + {file = "ruff-0.5.1-py3-none-win_amd64.whl", hash = "sha256:b1789bf2cd3d1b5a7d38397cac1398ddf3ad7f73f4de01b1e913e2abc7dfc51d"}, + {file = "ruff-0.5.1-py3-none-win_arm64.whl", hash = "sha256:2875b7596a740cbbd492f32d24be73e545a4ce0a3daf51e4f4e609962bfd3cd2"}, + {file = "ruff-0.5.1.tar.gz", hash = "sha256:3164488aebd89b1745b47fd00604fb4358d774465f20d1fcd907f9c0fc1b0655"}, ] [[package]] @@ -198,4 +199,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "e2477b2e2162ec2a5bcf673756341f3aa6f2bdb83c5438b6c3bc8f7fced226e2" +content-hash = "6ebb0770da484a771f8c11e7ba8100b3180b80c107e8b56d240036e7d2689506" diff --git a/pyproject.toml b/pyproject.toml index 136ed75..2d29e39 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ sqlalchemy = "^2.0.31" [tool.poetry.group.dev.dependencies] -ruff = "^0.4.4" +ruff = "0.5.1" [build-system] requires = ["poetry-core"] diff --git a/shell.nix b/shell.nix index 7be5d43..3fe8424 100644 --- a/shell.nix +++ b/shell.nix @@ -6,44 +6,40 @@ ... }: - let - inherit (inputs) nixpkgs; - pkgs = nixpkgs.legacyPackages.${system}; +let + inherit (inputs) nixpkgs; + pkgs = nixpkgs.legacyPackages.${system}; - # construct the shell provided by pre-commit for running hooks - pre-commit = pkgs.mkShell { - inherit (checks.pre-commit-check) shellHook; - buildInputs = checks.pre-commit-check.enabledPackages; - }; + # construct the shell provided by pre-commit for running hooks + pre-commit = pkgs.mkShell { + inherit (checks.pre-commit-check) shellHook; + buildInputs = checks.pre-commit-check.enabledPackages; + }; - # constructs a custom shell with commonly used utilities - rad-dev = pkgs.mkShell { - packages = with pkgs; [ - deadnix - pre-commit - treefmt - statix - nixfmt-rfc-style - ]; - }; + # constructs a custom shell with commonly used utilities + rad-dev = pkgs.mkShell { + packages = with pkgs; [ + deadnix + pre-commit + treefmt + statix + nixfmt-rfc-style + ]; + }; - # constructs the application in-place - app = pkgs.mkShell{ - inputsFrom = [self.packages.${system}.myapp]; - }; + # constructs the application in-place + app = pkgs.mkShell { inputsFrom = [ self.packages.${system}.myapp ]; }; - # pull in python/poetry dependencies - poetry = pkgs.mkShell { - pacakges = [pkgs.poetry]; - }; - in - { - default = pkgs.mkShell { - inputsFrom = [ - pre-commit - rad-dev - app - poetry - ]; - }; - } + # pull in python/poetry dependencies + poetry = pkgs.mkShell { packages = [ pkgs.poetry ]; }; +in +{ + default = pkgs.mkShell { + inputsFrom = [ + pre-commit + rad-dev + app + poetry + ]; + }; +} diff --git a/treefmt.toml b/treefmt.toml new file mode 100644 index 0000000..2ae50b8 --- /dev/null +++ b/treefmt.toml @@ -0,0 +1,22 @@ +# One CLI to format the code tree - https://github.com/numtide/treefmt + +[global] +# Glob patterns of files to exclude +excludes = [ ".git/" ] + + +[formatter.nixfmt] +# Formatter to run +command = "nixfmt" +# Command-line arguments for the command +#options = [] +# Glob pattern of files to include +includes = [ "*.nix" ] + +[formatter.ruff] +# Formatter to run +command = "ruff" +# Command-line arguments for the command +options = ["format"] +# Glob pattern of files to include +includes = [ "*.py" ]