Skip to content

Commit 82af756

Browse files
authored
Merge pull request #36 from Haskell-OpenAPI-Code-Generator/nix-function
First draft of the nix client generator function
2 parents f2db166 + 523ff12 commit 82af756

8 files changed

+149
-17
lines changed

default.nix

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
let
2-
nix-pre-commit-hooks = import (builtins.fetchTarball "https://github.com/cachix/pre-commit-hooks.nix/tarball/master");
3-
in {
4-
pre-commit-check = nix-pre-commit-hooks.run {
5-
src = ./.;
6-
hooks = {
7-
ormolu = {
8-
enable = true;
9-
excludes = [".circleci/golden"];
10-
};
11-
hlint = {
12-
enable = true;
13-
excludes = [".circleci/golden"];
14-
};
15-
};
16-
};
17-
}
1+
let
2+
nix-pre-commit-hooks = import (builtins.fetchTarball "https://github.com/cachix/pre-commit-hooks.nix/tarball/master");
3+
in
4+
{
5+
pre-commit-check = nix-pre-commit-hooks.run {
6+
src = ./.;
7+
hooks = {
8+
ormolu = {
9+
enable = true;
10+
excludes = [ ".circleci/golden" ];
11+
};
12+
hlint = {
13+
enable = true;
14+
excludes = [ ".circleci/golden" ];
15+
};
16+
nixpkgs-fmt.enable = true;
17+
};
18+
};
19+
}

nix/example-client.nix

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(import ./pkgs.nix).generateOpenAPIClient
2+
{
3+
name = "stripe-api";
4+
src = ../.circleci/specifications/stripe-api.yml;
5+
packageName = "stripe-api";
6+
moduleName = "StripeAPI";
7+
extraFlags = [
8+
"--property-type-suffix=\"'\""
9+
"--convert-to-camel-case"
10+
];
11+
operations = [
12+
"GetEvents"
13+
"GetCustomers"
14+
"PostCheckoutSessions"
15+
];
16+
}

nix/generate-client.nix

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{ pkgs ? import ./pkgs.nix
2+
}:
3+
{ name
4+
, src
5+
, packageName ? name
6+
, moduleName
7+
, extraFlags ? [ ]
8+
, operations ? [ ] # Empty list means "All operations"
9+
}:
10+
11+
with pkgs.lib;
12+
with pkgs.haskell.lib;
13+
let
14+
localPkgs = import ./pkgs.nix;
15+
extraFlagsStr = concatStringsSep " " extraFlags;
16+
operationFlag = operation: "--operation-to-generate '${operation}'";
17+
operationsFlags = optionalString (operations != [ ]) ''
18+
--omit-additional-operation-functions ${concatStringsSep " " (map operationFlag operations)}
19+
'';
20+
generatedCode = pkgs.stdenv.mkDerivation {
21+
name = "generated-${name}";
22+
buildInputs = [
23+
localPkgs.haskellPackages.openapi3-code-generator
24+
];
25+
buildCommand = ''
26+
# To make sure that we don't get issues with encodings
27+
export LANG=C.utf8
28+
export LC_ALL=C.utf8
29+
30+
set -x
31+
32+
openapi3-code-generator-exe ${src} \
33+
--module-name "${moduleName}" \
34+
--package-name "${packageName}" \
35+
--output-dir "$out" \
36+
${extraFlagsStr} \
37+
${operationsFlags}
38+
39+
set +x
40+
'';
41+
};
42+
generatedPackage = dontHaddock (disableLibraryProfiling (pkgs.haskellPackages.callCabal2nix name generatedCode { }));
43+
in
44+
{
45+
code = generatedCode;
46+
package = generatedPackage;
47+
}

nix/gitignore-src.nix

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
final: previous:
2+
let
3+
gitignoreSrc =
4+
final.fetchFromGitHub {
5+
owner = "hercules-ci";
6+
repo = "gitignore.nix";
7+
rev = "c4662e662462e7bf3c2a968483478a665d00e717";
8+
sha256 = "sha256:1npnx0h6bd0d7ql93ka7azhj40zgjp815fw2r6smg8ch9p7mzdlx";
9+
};
10+
in
11+
{
12+
inherit (import gitignoreSrc { inherit (final) lib; }) gitignoreSource;
13+
}

nix/nixpkgs-version.nix

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
rev = "e3a2247046d4de66b9b193a6ab3ff040fa3da86d";
3+
sha256 = "sha256:0gnpspk2lhwjfmkim416az7q3p5rjbc7q5pvhq23j4gbgkhs0q6i";
4+
}

nix/nixpkgs.nix

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let
2+
nixpkgsVersion = import ./nixpkgs-version.nix;
3+
in
4+
builtins.fetchTarball {
5+
url =
6+
"https://github.com/NixOS/nixpkgs/archive/${nixpkgsVersion.rev}.tar.gz";
7+
inherit (nixpkgsVersion) sha256;
8+
}

nix/overlay.nix

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
final: previous:
2+
with final.lib;
3+
with final.haskell.lib;
4+
let
5+
in
6+
{
7+
generateOpenAPIClient = import ./generate-client.nix { pkgs = final; };
8+
haskellPackages =
9+
previous.haskellPackages.override (
10+
old:
11+
{
12+
overrides =
13+
final.lib.composeExtensions
14+
(
15+
old.overrides or (
16+
_:
17+
_:
18+
{ }
19+
)
20+
)
21+
(
22+
self: super:
23+
{
24+
openapi3-code-generator = final.haskellPackages.callCabal2nix "openapi3-code-generator" (final.gitignoreSource ../.) { };
25+
}
26+
);
27+
}
28+
);
29+
}

nix/pkgs.nix

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let
2+
pkgsv = import (import ./nixpkgs.nix);
3+
pkgs = pkgsv { };
4+
ourPkgs =
5+
pkgsv {
6+
overlays =
7+
[
8+
(import ./gitignore-src.nix)
9+
(import ./overlay.nix)
10+
];
11+
};
12+
in
13+
ourPkgs

0 commit comments

Comments
 (0)