Skip to content

First draft of the nix client generator function #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
let
nix-pre-commit-hooks = import (builtins.fetchTarball "https://github.com/cachix/pre-commit-hooks.nix/tarball/master");
in {
pre-commit-check = nix-pre-commit-hooks.run {
src = ./.;
hooks = {
ormolu = {
enable = true;
excludes = [".circleci/golden"];
};
hlint = {
enable = true;
excludes = [".circleci/golden"];
};
};
};
}
let
nix-pre-commit-hooks = import (builtins.fetchTarball "https://github.com/cachix/pre-commit-hooks.nix/tarball/master");
in
{
pre-commit-check = nix-pre-commit-hooks.run {
src = ./.;
hooks = {
ormolu = {
enable = true;
excludes = [ ".circleci/golden" ];
};
hlint = {
enable = true;
excludes = [ ".circleci/golden" ];
};
nixpkgs-fmt.enable = true;
};
};
}
16 changes: 16 additions & 0 deletions nix/example-client.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(import ./pkgs.nix).generateOpenAPIClient
{
name = "stripe-api";
src = ../.circleci/specifications/stripe-api.yml;
packageName = "stripe-api";
moduleName = "StripeAPI";
extraFlags = [
"--property-type-suffix=\"'\""
"--convert-to-camel-case"
];
operations = [
"GetEvents"
"GetCustomers"
"PostCheckoutSessions"
];
}
47 changes: 47 additions & 0 deletions nix/generate-client.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{ pkgs ? import ./pkgs.nix
}:
{ name
, src
, packageName ? name
, moduleName
, extraFlags ? [ ]
, operations ? [ ] # Empty list means "All operations"
}:

with pkgs.lib;
with pkgs.haskell.lib;
let
localPkgs = import ./pkgs.nix;
extraFlagsStr = concatStringsSep " " extraFlags;
operationFlag = operation: "--operation-to-generate '${operation}'";
operationsFlags = optionalString (operations != [ ]) ''
--omit-additional-operation-functions ${concatStringsSep " " (map operationFlag operations)}
'';
generatedCode = pkgs.stdenv.mkDerivation {
name = "generated-${name}";
buildInputs = [
localPkgs.haskellPackages.openapi3-code-generator
];
buildCommand = ''
# To make sure that we don't get issues with encodings
export LANG=C.utf8
export LC_ALL=C.utf8

set -x

openapi3-code-generator-exe ${src} \
--module-name "${moduleName}" \
--package-name "${packageName}" \
--output-dir "$out" \
${extraFlagsStr} \
${operationsFlags}

set +x
'';
};
generatedPackage = dontHaddock (disableLibraryProfiling (pkgs.haskellPackages.callCabal2nix name generatedCode { }));
in
{
code = generatedCode;
package = generatedPackage;
}
13 changes: 13 additions & 0 deletions nix/gitignore-src.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
final: previous:
let
gitignoreSrc =
final.fetchFromGitHub {
owner = "hercules-ci";
repo = "gitignore.nix";
rev = "c4662e662462e7bf3c2a968483478a665d00e717";
sha256 = "sha256:1npnx0h6bd0d7ql93ka7azhj40zgjp815fw2r6smg8ch9p7mzdlx";
};
in
{
inherit (import gitignoreSrc { inherit (final) lib; }) gitignoreSource;
}
4 changes: 4 additions & 0 deletions nix/nixpkgs-version.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
rev = "e3a2247046d4de66b9b193a6ab3ff040fa3da86d";
sha256 = "sha256:0gnpspk2lhwjfmkim416az7q3p5rjbc7q5pvhq23j4gbgkhs0q6i";
}
8 changes: 8 additions & 0 deletions nix/nixpkgs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let
nixpkgsVersion = import ./nixpkgs-version.nix;
in
builtins.fetchTarball {
url =
"https://github.com/NixOS/nixpkgs/archive/${nixpkgsVersion.rev}.tar.gz";
inherit (nixpkgsVersion) sha256;
}
29 changes: 29 additions & 0 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
final: previous:
with final.lib;
with final.haskell.lib;
let
in
{
generateOpenAPIClient = import ./generate-client.nix { pkgs = final; };
haskellPackages =
previous.haskellPackages.override (
old:
{
overrides =
final.lib.composeExtensions
(
old.overrides or (
_:
_:
{ }
)
)
(
self: super:
{
openapi3-code-generator = final.haskellPackages.callCabal2nix "openapi3-code-generator" (final.gitignoreSource ../.) { };
}
);
}
);
}
13 changes: 13 additions & 0 deletions nix/pkgs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let
pkgsv = import (import ./nixpkgs.nix);
pkgs = pkgsv { };
ourPkgs =
pkgsv {
overlays =
[
(import ./gitignore-src.nix)
(import ./overlay.nix)
];
};
in
ourPkgs