Skip to content

Commit

Permalink
nix: refactor library
Browse files Browse the repository at this point in the history
  • Loading branch information
pnmadelaine committed Sep 24, 2023
1 parent dd11320 commit 847e216
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 6 deletions.
12 changes: 6 additions & 6 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
};
};

outputs = {
outputs = inputs @ {
self,
flake-utils,
nixpkgs,
crane,
rust-overlay,
}:
flake-utils.lib.eachDefaultSystem (system: let
}: let
lib = import ./lib inputs;
in
flake-utils.lib.eachSystem lib.systems (system: let
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
Expand All @@ -42,7 +44,6 @@
pkgs.callPackage ./nix/api-client-test.nix {inherit craneLib;};
typhon-devshell =
pkgs.callPackage ./nix/devshell.nix {inherit rustToolchain;};
typhon-actions = import ./nix/actions {inherit pkgs;};
in {
packages = {
inherit typhon typhon-webapp typhon-doc typhon-api-client-test;
Expand All @@ -58,10 +59,9 @@
formatted = pkgs.callPackage ./nix/check-formatted.nix {inherit rustToolchain;};
nixos = pkgs.callPackage ./nix/nixos/test.nix {typhon = self;};
};

actions = typhon-actions;
})
// {
inherit lib;
nixosModules.default = import ./nix/nixos/typhon.nix self;
};
}
12 changes: 12 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
inputs: let
utils = import ./utils.nix inputs;
x = utils.importer "lib" [
./github
./mkAction.nix
./mkGitJobsets.nix
./mkProject.nix
./systems.nix
]
x;
in
x.lib
6 changes: 6 additions & 0 deletions lib/github/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
utils:
utils.importer "github" [
./mkGithubJobsets.nix
./mkGithubProject.nix
./mkGithubStatus.nix
]
34 changes: 34 additions & 0 deletions lib/github/mkGithubJobsets.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
utils: {lib}: let
inherit
(lib)
mkAction
;
in {
mkGithubJobsets = {
owner,
repo,
}:
mkAction (system: let
pkgs = utils.pkgs.${system};
in
pkgs.writeShellApplication {
name = "action";
runtimeInputs = [
pkgs.curl
pkgs.gnused
pkgs.jq
];
text = ''
input=$(cat)
token=$(echo "$input" | jq '.secrets.github_token' -r)
curl -s \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $token" \
https://api.github.com/repos/${owner}/${repo}/branches \
-k \
| jq --arg o "${owner}" --arg r "${repo}" 'map({ key: .name, value: { "flake": ("github:" + $o + "/" + $r + "/" + .name) }}) | from_entries'
'';
});
}
29 changes: 29 additions & 0 deletions lib/github/mkGithubProject.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
_: {lib}: let
inherit
(lib)
mkProject
;
inherit
(lib.github)
mkGithubJobsets
mkGithubStatus
;
in {
mkGithubProject = {
owner,
repo,
secrets,
title ? repo,
description ? "",
homepage ? "https://github.com/${owner}/${repo}",
}:
mkProject {
meta = {inherit title description homepage;};
actions = {
jobsets = mkGithubJobsets {inherit owner repo;};
begin = mkGithubStatus {inherit owner repo;};
end = mkGithubStatus {inherit owner repo;};
};
inherit secrets;
};
}
63 changes: 63 additions & 0 deletions lib/github/mkGithubStatus.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
utils: {lib}: let
inherit
(lib)
mkAction
;
in {
mkGithubStatus = {
owner,
repo,
}:
mkAction (system: let
pkgs = utils.pkgs.${system};
in
pkgs.writeShellApplication {
name = "action";
runtimeInputs = [
pkgs.curl
pkgs.gnused
pkgs.jq
];
text = ''
input=$(cat)
build=$(echo "$input" | jq '.input.build' -r)
data=$(echo "$input" | jq '.input.data' -r)
evaluation=$(echo "$input" | jq '.input.evaluation' -r)
flake_locked=$(echo "$input" | jq '.input.flake_locked' -r)
job=$(echo "$input" | jq '.input.job' -r)
jobset=$(echo "$input" | jq '.input.jobset' -r)
project=$(echo "$input" | jq '.input.project' -r)
status=$(echo "$input" | jq '.input.status' -r)
token=$(echo "$input" | jq '.secrets.github_token' -r)
rev=$(echo "$flake_locked" | sed 's/github:.*\/.*\/\(.*\)/\1/')
target_url="$(echo "$data" | jq '.url' -r)/builds/$build"
context="Typhon: $job"
description="$project:$jobset:$evaluation:$job"
case $status in
"error")
state="failure"
;;
"pending")
state="pending"
;;
"success")
state="success"
;;
*)
state="error"
;;
esac
curl -s \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $token" \
"https://api.github.com/repos/${owner}/${repo}/statuses/$rev" \
-d "{\"state\":\"$state\",\"target_url\":\"$target_url\",\"description\":\"$description\",\"context\":\"$context\"}" \
-k >&2
'';
});
}
8 changes: 8 additions & 0 deletions lib/mkAction.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
utils: {lib}: let
inherit
(lib)
systems
;
in {
mkAction = utils.lib.genAttrs systems;
}
29 changes: 29 additions & 0 deletions lib/mkGitJobsets.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
utils: {lib}: let
inherit
(lib)
mkAction
;
in {
mkGitJobsets = url:
mkAction (system: let
pkgs = utils.pkgs.${system};
in
pkgs.writeShellApplication {
name = "action";
runtimeInputs = [
pkgs.git
pkgs.gnused
pkgs.jq
];
text = ''
heads=$(git ls-remote --heads ${url} | sed 's/.*refs\/heads\/\(.*\)/\1/')
cmd=""
for head in $heads
do
cmd="$cmd . += {\"$head\": { \"flake\": \"git+${url}?ref=$head\" } } |"
done
array=$(echo "{}" | jq "$cmd .")
echo "$array"
'';
});
}
35 changes: 35 additions & 0 deletions lib/mkProject.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
utils: {lib}: let
inherit
(lib)
systems
;
in {
mkProject = args @ {
actions ? {},
meta ? {},
secrets ? null,
}: {
inherit meta;
actions = utils.lib.genAttrs systems (
system: let
pkgs = utils.pkgs.${system};
linkAction = name:
if actions ? ${name} && actions.${name} ? ${system}
then "ln -s ${actions.${name}.${system}}/bin/action ${name}"
else "";
linkSecrets =
if secrets != null
then "ln -s ${secrets} secrets"
else "";
in
pkgs.runCommand "actions" {} ''
mkdir $out
cd $out
${linkAction "jobsets"}
${linkAction "begin"}
${linkAction "end"}
${linkSecrets}
''
);
};
}
3 changes: 3 additions & 0 deletions lib/systems.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
utils: _: {
systems = utils.inputs.flake-utils.lib.defaultSystems;
}
18 changes: 18 additions & 0 deletions lib/utils.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
inputs @ {nixpkgs, ...}: let
self = rec {
inherit inputs;

lib = nixpkgs.lib;

pkgs = nixpkgs.legacyPackages;

importer = scope:
lib.foldr
(file: fn: lib:
self.lib.recursiveUpdate
{${scope} = import file self lib;}
(fn lib))
(_: {});
};
in
self
Empty file added secrets
Empty file.

0 comments on commit 847e216

Please sign in to comment.