Skip to content

Commit

Permalink
Bump nixpkgs and simplify/clean up nix setup (#1529)
Browse files Browse the repository at this point in the history
* Refactor tests and fix README

* fix linting hints from new hlint version

* postgrest-style with new nixpkgs-fmt
  • Loading branch information
monacoremo authored May 21, 2020
1 parent e6874c8 commit 48c9ac3
Show file tree
Hide file tree
Showing 27 changed files with 268 additions and 344 deletions.
8 changes: 5 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ jobs:
- checkout
- run:
name: Install linting and styling scripts
command: nix-env -f default.nix -iA lint style
command: nix-env -f default.nix -iA devtools
- run:
name: Run linter
command: |
echo 'Note: For checking this locally, use `make lint`. Or, if using `nix`, run `nix-shell --run postgrest-lint`'
# Note: For checking this locally, use `make lint`. Or, if using
# `nix`, run `nix-shell --run postgrest-lint`
postgrest-lint
- run:
name: Run style check
command: |
echo 'Note: For checking this locally, use `make style`. Or, if using `nix`, run `nix-shell --run postgrest-style`'
# 'Note: For checking this locally, use `make style`. Or, if using
# `nix`, run `nix-shell --run postgrest-style`
postgrest-style-check
build-test-9.4:
Expand Down
55 changes: 29 additions & 26 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,40 @@ let

overlays =
[
(import nix/overlays/postgresql-default.nix)
(import nix/overlays/postgresql-legacy.nix)
(import nix/overlays/gitignore.nix)
(import nix/overlays/haskell-packages)
(import nix/overlays/haskell-packages { inherit compiler; })
];

# Evaluated expression of the Nixpkgs repository.
pkgs =
import nixpkgs { inherit overlays; };

postgresqlVersions =
[
pkgs.postgresql_12
pkgs.postgresql_11
pkgs.postgresql_10
pkgs.postgresql_9_6
pkgs.postgresql_9_5
pkgs.postgresql_9_4
];
{
postgresql-12 = pkgs.postgresql_12;
postgresql-11 = pkgs.postgresql_11;
postgresql-10 = pkgs.postgresql_10;
"postgresql-9.6" = pkgs.postgresql_9_6;
"postgresql-9.5" = pkgs.postgresql_9_5;
"postgresql-9.4" = pkgs.postgresql_9_4;
};

patches =
pkgs.callPackage nix/patches {};
pkgs.callPackage nix/patches { };

# Base dynamic derivation for the PostgREST package.
drv =
lib.enableCabalFlag (pkgs.haskellPackages.callCabal2nix name src {}) "FailOnWarn";
pkgs.haskell.packages."${compiler}".callCabal2nix name src { };

# Static set of Haskell Packages based on nh2/static-haskell-nix
staticHaskellPackages =
import nix/static-haskell-packages.nix { inherit nixpkgs compiler patches; };

# Static derivation for the PostgREST executable.
drvStatic =
import nix/static { inherit nixpkgs name src compiler patches; };
staticHaskellPackages.callCabal2nix name src { };

lib =
pkgs.haskell.lib;
Expand All @@ -67,15 +72,15 @@ rec {
# Derivation for the PostgREST Haskell package, including the executable,
# libraries and documentation. We disable running the test suite on Nix
# builds, as they require a database to be set up.
postgrestWithLib =
lib.dontCheck drv;
postgrestPackage =
lib.dontCheck (lib.enableCabalFlag drv "FailOnWarn");

# Derivation for just the PostgREST binary, where we strip all dynamic
# libraries and documentation, leaving only the executable. Note that the
# executable is static with regards to Haskell libraries, but not system
# libraries like glibc and libpq.
postgrest =
lib.justStaticExecutables postgrestWithLib;
lib.justStaticExecutables postgrestPackage;

# Static executable.
postgrestStatic =
Expand All @@ -98,18 +103,16 @@ rec {

# Utility for updating the pinned version of Nixpkgs.
nixpkgsUpgrade =
pkgs.callPackage nix/nixpkgs-upgrade.nix {};
pkgs.callPackage nix/nixpkgs-upgrade.nix { };

# Scripts for running tests.
tests =
pkgs.callPackage nix/tests.nix
{
inherit postgresqlVersions;
postgrestBuildEnv = env;
};

style =
pkgs.callPackage nix/style.nix {};
pkgs.callPackage nix/tests.nix {
inherit postgresqlVersions;
postgrestBuildEnv = env;
};

lint =
pkgs.callPackage nix/lint.nix {};
# Development tools, including linting and styling scripts.
devtools =
pkgs.callPackage nix/devtools.nix { };
}
39 changes: 27 additions & 12 deletions nix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ build the `postgrest` attribute from the Nix expression it finds in our
`default.nix` (see below for details). Nix will take care of getting the right
GHC version and all the build dependencies.

We recommend that you use the PostgREST binary cache on
[cachix](https://cachix.org/):

```
# Install cachix:
nix-env -iA cachix -f https://cachix.org/api/v1/install
# Set cachix up to use the PostgREST binary cache:
cachix use postgrest
```

Without cachix, your machine will have to rebuild all the dependencies that are
derived on top of `Musl` for the static builds, which can take a very long time.

## Developing

A development environment for PostgREST is available with `nix-shell`. The
Expand All @@ -44,21 +59,21 @@ dependencies from the same pinned Nixpkgs version that the Nix builds use.

## Aside: Working with `nix-shell` and the PostgREST utility scripts

The PostgREST utilities available in `nix-shell` all have names that begin
with `postgrest-`, so you can use tab completion (`postgrest-<tab>`) in
`nix-shell` to see all that are available:
The PostgREST utilities available in `nix-shell` all have names that begin with
`postgrest-`, so you can use tab completion (typing `postgrest-` and pressing
`<tab>`) in `nix-shell` to see all that are available:

```
# Note: The utilities listed here might not be up to date.
[nix-shell:~/Projects/postgrest]$ postgrest-
postgrest-lint postgrest-test-spec-postgresql-10.12
postgrest-style postgrest-test-spec-postgresql-11.7
postgrest-style-check postgrest-test-spec-postgresql-12.2
postgrest-test-all postgrest-test-spec-postgresql-9.4.24
postgrest-test-spec postgrest-test-spec-postgresql-9.5.21
postgrest-test-spec-all postgrest-test-spec-postgresql-9.6.17
[nix-shell:~/Projects/postgrest]$
[nix-shell]$ postgrest-<tab>
postgrest-lint postgrest-test-spec-postgresql-10
postgrest-style postgrest-test-spec-postgresql-11
postgrest-style-check postgrest-test-spec-postgresql-12
postgrest-test-all postgrest-test-spec-postgresql-9.4
postgrest-test-spec postgrest-test-spec-postgresql-9.5
postgrest-test-spec-all postgrest-test-spec-postgresql-9.6
[nix-shell]$
```

Expand Down
18 changes: 15 additions & 3 deletions nix/style.nix → nix/devtools.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{ writeShellScriptBin
, buildEnv
, git
, hlint
, nixpkgs-fmt
, silver-searcher
, stylish-haskell
, buildEnv
}:
let
style =
Expand All @@ -30,8 +31,19 @@ let
${git}/bin/git diff-index --exit-code HEAD -- '*.hs' '*.lhs' '*.nix'
'';
lint =
writeShellScriptBin "postgrest-lint"
''
set -euo pipefail
rootdir="$(${git}/bin/git rev-parse --show-toplevel)"
# Lint Haskell files
${silver-searcher}/bin/ag -l -g '\.l?hs$' "$rootdir" \
| xargs ${hlint}/bin/hlint -X QuasiQuotes -X NoPatternSynonyms
'';
in
buildEnv {
name = "postgrest-style";
paths = [ style check ];
name = "postgrest-devtools";
paths = [ style check lint ];
}
72 changes: 36 additions & 36 deletions nix/docker/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@
let
image =
tag:
dockerTools.buildImage {
inherit tag;
dockerTools.buildImage {
inherit tag;

name = "postgrest/postgrest";
contents = postgrest;
name = "postgrest/postgrest";
contents = postgrest;

# Set the current time as the image creation date. This makes the build
# non-reproducible, but that should not be an issue for us.
created = "now";
# Set the current time as the image creation date. This makes the build
# non-reproducible, but that should not be an issue for us.
created = "now";

extraCommands =
''
mkdir etc
cp ${./postgrest.conf} etc/postgrest.conf
'';
extraCommands =
''
mkdir etc
cp ${./postgrest.conf} etc/postgrest.conf
'';

config = {
Cmd = [ "/bin/postgrest" "/etc/postgrest.conf" ];
Env = [
"PGRST_DB_URI=postgresql://?user=postgres"
"PGRST_DB_SCHEMA=public"
"PGRST_DB_ANON_ROLE="
"PGRST_DB_POOL=100"
"PGRST_DB_EXTRA_SEARCH_PATH=public"
"PGRST_SERVER_HOST=*4"
"PGRST_SERVER_PORT=3000"
"PGRST_OPENAPI_SERVER_PROXY_URI="
"PGRST_JWT_SECRET="
"PGRST_SECRET_IS_BASE64=false"
"PGRST_JWT_AUD="
"PGRST_MAX_ROWS="
"PGRST_PRE_REQUEST="
"PGRST_ROLE_CLAIM_KEY=.role"
"PGRST_ROOT_SPEC="
"PGRST_RAW_MEDIA_TYPES="
];
User = "1000";
ExposedPorts = {
"3000/tcp" = {};
};
config = {
Cmd = [ "/bin/postgrest" "/etc/postgrest.conf" ];
Env = [
"PGRST_DB_URI=postgresql://?user=postgres"
"PGRST_DB_SCHEMA=public"
"PGRST_DB_ANON_ROLE="
"PGRST_DB_POOL=100"
"PGRST_DB_EXTRA_SEARCH_PATH=public"
"PGRST_SERVER_HOST=*4"
"PGRST_SERVER_PORT=3000"
"PGRST_OPENAPI_SERVER_PROXY_URI="
"PGRST_JWT_SECRET="
"PGRST_SECRET_IS_BASE64=false"
"PGRST_JWT_AUD="
"PGRST_MAX_ROWS="
"PGRST_PRE_REQUEST="
"PGRST_ROLE_CLAIM_KEY=.role"
"PGRST_ROOT_SPEC="
"PGRST_RAW_MEDIA_TYPES="
];
User = "1000";
ExposedPorts = {
"3000/tcp" = { };
};
};
};
in
rec {
imageLatest =
Expand Down
12 changes: 0 additions & 12 deletions nix/lint.nix

This file was deleted.

3 changes: 2 additions & 1 deletion nix/nixpkgs-upgrade.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ let
tarballUrlBase =
https://github.com/nixos/nixpkgs/archive/;
in
writeShellScriptBin name
writeShellScriptBin
name
''
set -euo pipefail
Expand Down
6 changes: 3 additions & 3 deletions nix/nixpkgs-version.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Pinned version of Nixpkgs, generated with nixpkgs-upgrade.
{
date = "2020-04-19";
rev = "10100a97c8964e82b30f180fda41ade8e6f69e41";
tarballHash = "011f36kr3c1ria7rag7px26bh73d1b0xpqadd149bysf4hg17rln";
date = "2020-05-19";
rev = "0f5ce2fac0c726036ca69a5524c59a49e2973dd4";
tarballHash = "0nkk492aa7pr0d30vv1aw192wc16wpa1j02925pldc09s9m9i0r3";
}
3 changes: 2 additions & 1 deletion nix/overlays/gitignore.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ self: super:
rev = "2ced4519f865341adcb143c5d668f955a2cb997f";
sha256 = "sha256:0fc5bgv9syfcblp23y05kkfnpgh3gssz6vn24frs8dzw39algk2z";
};
in (super.callPackage gitignoreSrc {}).gitignoreSource;
in
(super.callPackage gitignoreSrc { }).gitignoreSource;
}
File renamed without changes.
6 changes: 3 additions & 3 deletions nix/overlays/haskell-packages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ The `.nix` files in this directory pin specific versions of Haskell packages
from Hackage. They were generated with `cabal2nix`, e.g.:

```bash
cabal2nix cabal://protolude > protolude.nix
cabal2nix cabal://Cabal-3.0.0.0 > Cabal.nix

```

Those overrides might become obsolete as the pinned versions are adopted into
our pinned version of nixpkgs.
Those overrides will likely become obsolete as the pinned versions are adopted
into our pinned version of nixpkgs.
38 changes: 32 additions & 6 deletions nix/overlays/haskell-packages/default.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
{ compiler, static ? false }:

self: super:
let
lib =
self.haskell.lib;

overrides =
final: prev:
rec {
protolude =
prev.callHackageDirect
{
pkg = "protolude";
ver = "0.3.0";
sha256 = "0iwh4wsjhb7pms88lw1afhdal9f86nrrkkvv65f9wxbd1b159n72";
} { };

cabal2nix =
if static then
# cabal2nix depends on Cabal 3.0.*, while our pinned version of
# Nixpkgs only provides 2.4 or 3.2. So we pinned 3.0.0.0 in
# ./Cabal.nix
prev.cabal2nix.overrideScope (self: super: { Cabal = self.callPackage ./Cabal.nix { }; })
else
prev.cabal2nix;
};
in
{
haskellPackages =
super.haskellPackages.override {
overrides =
final: prev: rec {
protolude = prev.callPackage ./protolude.nix {};
};
haskell =
super.haskell // {
packages = super.haskell.packages // {
"${compiler}" =
super.haskell.packages."${compiler}".override { inherit overrides; };
};
};
}
Loading

0 comments on commit 48c9ac3

Please sign in to comment.