Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Add nixos service and nixos test.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbgi committed Jun 16, 2020
1 parent e72075f commit d2b3cba
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 6 deletions.
4 changes: 4 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ let
tests = collectChecks haskellPackages;
};

nixosTests = import ./nix/nixos/tests {
inherit pkgs;
};

shell = import ./shell.nix {
inherit pkgs;
withHoogle = true;
Expand Down
53 changes: 53 additions & 0 deletions nix/nixos/smash-service.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{ config, lib, pkgs, ... }:

let
cfg = config.services.smash;
in {

options = {
services.smash = {
enable = lib.mkEnableOption "enable the smash server";
script = lib.mkOption {
internal = true;
type = lib.types.package;
};
package = lib.mkOption {
type = lib.types.package;
default = (import ../. {}).smashHaskellPackages.smash.components.exes.smash-exe;
};
};
};
config = lib.mkIf cfg.enable {
services.smash.script = pkgs.writeShellScript "smash" ''
set -euo pipefail
cp ${config.services.cardano-db-sync.pgpass} $RUNTIME_DIRECTORY/pgpass
chmod 0600 $RUNTIME_DIRECTORY/pgpass
export PGPASSFILE=$RUNTIME_DIRECTORY/pgpass
${cfg.package}/bin/smash-exe run-migrations --mdir ${../../schema}
exec ${cfg.package}/bin/smash-exe run-app
'';
environment.systemPackages = [ cfg.package config.services.postgresql.package ];
systemd.services.smash = {
path = [ cfg.package config.services.postgresql.package pkgs.netcat ];
preStart = ''
for x in {1..10}; do
nc -z localhost ${toString config.services.cardano-db-sync.postgres.port} && break
echo loop $x: waiting for postgresql 2 sec...
sleep 2
done
sleep 1
'';
serviceConfig = {
ExecStart = config.services.smash.script;
DynamicUser = true;
RuntimeDirectory = "smash";
};

wantedBy = [ "multi-user.target" ];
after = [ "postgres.service" "cardano-db-sync.service" ];
requires = [ "postgresql.service" ];
};
};
}
20 changes: 20 additions & 0 deletions nix/nixos/tests/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{ pkgs
, supportedSystems ? [ "x86_64-linux" ]
}:

with pkgs;
with pkgs.commonLib;

let
forAllSystems = genAttrs supportedSystems;
importTest = fn: args: system: let
imported = import fn;
test = import (pkgs.path + "/nixos/tests/make-test.nix") imported;
in test ({
inherit pkgs system config;
} // args);
callTest = fn: args: forAllSystems (system: hydraJob (importTest fn args system));
in rec {

smashTest = callTest ./smash-test.nix {};
}
61 changes: 61 additions & 0 deletions nix/nixos/tests/smash-test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{ pkgs, ... }:
with pkgs; with commonLib;
{
name = "smash-test";
nodes = {
machine = { config, ... }: {
nixpkgs.pkgs = pkgs;
environment = {
systemPackages = with pkgs; [ curl jq ];
variables = {
PGPASSFILE = config.services.cardano-db-sync.pgpass;
};
};
imports = [
../smash-service.nix
(sources.cardano-db-sync + "/nix/nixos")
];
services.cardano-db-sync = {
enable = true;
cluster = "ff";
};
services.smash = {
enable = true;
package = smashHaskellPackages.smash.components.exes.smash-exe;
};
services.postgresql = {
enable = true;
package = postgresql_12;
enableTCPIP = false;
ensureDatabases = [ "${config.services.cardano-db-sync.user}" ];
ensureUsers = [
{
name = "${config.services.cardano-db-sync.user}";
ensurePermissions = {
"DATABASE ${config.services.cardano-db-sync.user}" = "ALL PRIVILEGES";
};
}
];
identMap = ''
cdbsync-users root ${config.services.cardano-db-sync.user}
cdbsync-users smash ${config.services.cardano-db-sync.user}
cdbsync-users ${config.services.cardano-db-sync.user} ${config.services.cardano-db-sync.user}
cdbsync-users postgres postgres
'';
authentication = ''
local all all ident map=cdbsync-users
'';
};
};
};
testScript = ''
startAll
$machine->waitForUnit("postgresql.service");
$machine->waitForUnit("smash.service");
$machine->waitForOpenPort(3100);
$machine->succeed("smash-exe insert-pool --filepath ${../../../test_pool.json} --poolhash \"cbdfc4f21feb0a414b2b9471fa56b0ebd312825e63db776d68cc3fa0ca1f5a2f\"");
$machine->succeed("curl -s -i -H \"Accept: application/json\" http://localhost:3100/api/v1/metadata/cbdfc4f21feb0a414b2b9471fa56b0ebd312825e63db776d68cc3fa0ca1f5a2f | systemd-cat --identifier=curl-smash");
$machine->succeed("curl -s -H \"Accept: application/json\" http://localhost:3100/api/v1/metadata/cbdfc4f21feb0a414b2b9471fa56b0ebd312825e63db776d68cc3fa0ca1f5a2f | jq | systemd-cat --identifier=jq-smash");
'';

}
24 changes: 18 additions & 6 deletions nix/sources.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
{
"cardano-db-sync": {
"branch": "master",
"description": "A component that follows the Cardano chain and stores blocks and transactions in PostgreSQL",
"homepage": "",
"owner": "input-output-hk",
"repo": "cardano-db-sync",
"rev": "6d2917212848a7f70e7e84336b9291dd2634ea61",
"sha256": "0bi6v6b9c2i2d3h416lpn0p2qy8pd08w0g0zjjsnldr2sb7ph3s9",
"type": "tarball",
"url": "https://github.com/input-output-hk/cardano-db-sync/archive/6d2917212848a7f70e7e84336b9291dd2634ea61.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"haskell.nix": {
"branch": "master",
"description": "Alternative Haskell Infrastructure for Nixpkgs",
"homepage": "https://input-output-hk.github.io/haskell.nix",
"owner": "input-output-hk",
"repo": "haskell.nix",
"rev": "c474dcec4a435698cd7978ef25e6fea25ff6795b",
"sha256": "149n40p56njpzibbkxlqj7rv8f2yvgfqni9m6xr8rvl5yry1lxd0",
"rev": "36836c1f580a021e99e7ad3ad23f4257e7ffda2b",
"sha256": "0jsg3lairnfn9i74dj6r88n53y5ca724wv9lvak67cbhmxgj35df",
"type": "tarball",
"url": "https://github.com/input-output-hk/haskell.nix/archive/c474dcec4a435698cd7978ef25e6fea25ff6795b.tar.gz",
"url": "https://github.com/input-output-hk/haskell.nix/archive/36836c1f580a021e99e7ad3ad23f4257e7ffda2b.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"iohk-nix": {
Expand All @@ -17,10 +29,10 @@
"homepage": null,
"owner": "input-output-hk",
"repo": "iohk-nix",
"rev": "8b1d65ba294708b12d7b15103ac35431d9b60819",
"sha256": "1z23lw28s3wa5bf5yr89i61m413ad299lyhv02i9r36p28wjl94g",
"rev": "a3eae81cbae000bbfdc2bc1880528512747a3a9c",
"sha256": "1jairn2vp43z36j0b1fi2nmnfpadff217dqv5bjjxm5v099g0gsw",
"type": "tarball",
"url": "https://github.com/input-output-hk/iohk-nix/archive/8b1d65ba294708b12d7b15103ac35431d9b60819.tar.gz",
"url": "https://github.com/input-output-hk/iohk-nix/archive/a3eae81cbae000bbfdc2bc1880528512747a3a9c.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}
3 changes: 3 additions & 0 deletions release.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ let

jobs = {
native = mapTestOn (__trace (__toJSON (packagePlatforms project)) (packagePlatforms project));
# only build nixos tests on first supported system (linux)
inherit (pkgsFor (builtins.head supportedSystems)) nixosTests;
} // (mkRequiredJob (concatLists [
(collectJobs jobs.native.checks)
(collectJobs jobs.native.libs)
(collectJobs jobs.native.exes)
(collectJobs jobs.nixosTests.smashTest)
]));

in jobs

0 comments on commit d2b3cba

Please sign in to comment.