From 0956440b2adbf878a4430ccea9a234e40bfb45b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Sun, 28 Aug 2022 15:00:19 +0200 Subject: [PATCH 1/4] add flake --- flake.lock | 48 ++++++++++++++++++++++++ flake.nix | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..fb6ad93 --- /dev/null +++ b/flake.lock @@ -0,0 +1,48 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1661009076, + "narHash": "sha256-phAE40gctVygRq3G3B6LhvD7u2qdQT21xsz8DdRDYFo=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "850d8a76026127ef02f040fb0dcfdb8b749dd9d9", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1661450036, + "narHash": "sha256-0/9UyJLtfWqF4uvOrjFIzk8ue1YYUHa6JIhV0mALkH0=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "f3d0897be466aa09a37f6bf59e62c360c3f9a6cc", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..2318451 --- /dev/null +++ b/flake.nix @@ -0,0 +1,108 @@ +{ + description = "Nix CMake Template"; + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + flake-parts = { + url = "github:hercules-ci/flake-parts"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = { self, flake-parts, ... }: + flake-parts.lib.mkFlake { inherit self; } { + systems = [ "x86_64-linux" ]; + imports = [ + ]; + perSystem = { config, self', inputs', pkgs, system, ... }: + let + pkgs = inputs'.nixpkgs.legacyPackages; + inherit (pkgs) lib; + + makeStatic = + let + staticPostgresql = (pkgs.postgresql_11.overrideAttrs (o: { + dontDisableStatic = true; + # https://www.postgresql-archive.org/building-libpq-a-static-library-td5970933.html + postConfigure = o.postConfigure + '' + echo -e 'libpq.a: $(OBJS)\n\tar rcs $@ $^' >> ./src/interfaces/libpq/Makefile + ''; + buildPhase = '' + make world + make -C ./src/interfaces/libpq libpq.a + ''; + postInstall = o.postInstall + '' + cp src/interfaces/libpq/libpq.a $out/lib/ + cp src/interfaces/libpq/libpq.a $lib/lib/ + ''; + })).override { gssSupport = false; }; + + staticPqxx = (pkgs.libpqxx.overrideAttrs (o: { configureFlags = [ ]; })).override { + stdenv = pkgs.makeStaticLibraries pkgs.stdenv; + postgresql = staticPostgresql; + }; + staticOpenssl = pkgs.openssl.override { static = true; }; + in + drv: (drv.override { + static = true; + libpqxx = staticPqxx; + }).overrideAttrs (o: { + buildInputs = o.buildInputs ++ [ + pkgs.glibc.static + staticOpenssl + staticPostgresql + ]; + doCheck = false; + }); + + serverVariants = + let + fromInputs = { stdenv, boost16x, static }: + let + p = self'.packages.mdb-server; + noDots = lib.replaceChars [ "." ] [ "_" ]; + staticStr = lib.optionalString static "-static"; + name = "${p.name}-${noDots stdenv.cc.cc.name}-${noDots boost16x.name}${staticStr}"; + maybeMakeStatic = drv: if static then makeStatic drv else drv; + drv = maybeMakeStatic (p.override { inherit boost16x stdenv; }); + in + lib.nameValuePair name drv; + + inputVariants = lib.cartesianProductOfSets { + stdenv = with pkgs; + map (overrideCC stdenv) [ gcc9 gcc10 gcc11 ] + ++ map (overrideCC clangStdenv) [ clang_11 clang_12 clang_13 ]; + boost16x = with pkgs; [ boost168 boost169 ]; + static = [ true false ]; + }; + in + builtins.listToAttrs (builtins.map fromInputs inputVariants); + + integrationTests = + let integrationTest = mdbServer: import ./integration_test.nix { + inherit pkgs mdbServer; + mdbWebservice = self'.packages.mdb-webserver; + }; + in + pkgs.lib.mapAttrs' (k: v: pkgs.lib.nameValuePair ("integrationtest-${k}") (integrationTest v)); + in + { + packages = { + mdb-server = pkgs.python3Packages.callPackage ./server/derivation.nix { }; + mdb-server-static = makeStatic self'.packages.mdb-server; + mdb-server-no-python = + let # this is useful because libpqxx with python support depends on python2.7 + # but we don't want python in its closure if we package it into a small + # docker image! + libpqxxWithoutPython = pkgs.libpqxx.override { + postgresql = pkgs.postgresql.override { + libxml2 = pkgs.libxml2.override { pythonSupport = false; }; + }; + }; + in self'.packages.mdb-server.override { libpqxx = libpqxxWithoutPython; }; + mdb-webserver = pkgs.python3Packages.callPackage ./python_client/derivation.nix { }; + }; + checks = { } // (integrationTests serverVariants); + + }; + }; +} From d191845b05da6d579b6bbe2a8a09b708d5762b6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Sun, 28 Aug 2022 15:17:52 +0200 Subject: [PATCH 2/4] fix infinite recursion --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 2318451..55f92c7 100644 --- a/flake.nix +++ b/flake.nix @@ -58,7 +58,7 @@ let fromInputs = { stdenv, boost16x, static }: let - p = self'.packages.mdb-server; + p = pkgs.python3Packages.callPackage ./server/derivation.nix { }; noDots = lib.replaceChars [ "." ] [ "_" ]; staticStr = lib.optionalString static "-static"; name = "${p.name}-${noDots stdenv.cc.cc.name}-${noDots boost16x.name}${staticStr}"; From f6009775861550d5acbaa660b9cfd2266c846bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Sun, 28 Aug 2022 15:25:35 +0200 Subject: [PATCH 3/4] add serverVariants to packages output --- flake.nix | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/flake.nix b/flake.nix index 55f92c7..c604df6 100644 --- a/flake.nix +++ b/flake.nix @@ -86,9 +86,7 @@ pkgs.lib.mapAttrs' (k: v: pkgs.lib.nameValuePair ("integrationtest-${k}") (integrationTest v)); in { - packages = { - mdb-server = pkgs.python3Packages.callPackage ./server/derivation.nix { }; - mdb-server-static = makeStatic self'.packages.mdb-server; + packages = serverVariants // { mdb-server-no-python = let # this is useful because libpqxx with python support depends on python2.7 # but we don't want python in its closure if we package it into a small @@ -98,7 +96,7 @@ libxml2 = pkgs.libxml2.override { pythonSupport = false; }; }; }; - in self'.packages.mdb-server.override { libpqxx = libpqxxWithoutPython; }; + in (pkgs.python3Packages.callPackage ./server/derivation.nix { }).override { libpqxx = libpqxxWithoutPython; }; mdb-webserver = pkgs.python3Packages.callPackage ./python_client/derivation.nix { }; }; checks = { } // (integrationTests serverVariants); From 89cb54bc228a2af3c70ce13fad946d036fa5980f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Sun, 28 Aug 2022 15:38:04 +0200 Subject: [PATCH 4/4] add dockerImages --- flake.nix | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/flake.nix b/flake.nix index c604df6..856fc2e 100644 --- a/flake.nix +++ b/flake.nix @@ -8,7 +8,7 @@ }; }; - outputs = { self, flake-parts, ... }: + outputs = { self, flake-parts, nixpkgs, ... }: flake-parts.lib.mkFlake { inherit self; } { systems = [ "x86_64-linux" ]; imports = [ @@ -58,7 +58,7 @@ let fromInputs = { stdenv, boost16x, static }: let - p = pkgs.python3Packages.callPackage ./server/derivation.nix { }; + p = pkgs.callPackage ./server/derivation.nix { }; noDots = lib.replaceChars [ "." ] [ "_" ]; staticStr = lib.optionalString static "-static"; name = "${p.name}-${noDots stdenv.cc.cc.name}-${noDots boost16x.name}${staticStr}"; @@ -87,6 +87,7 @@ in { packages = serverVariants // { + mdb-server = pkgs.callPackage ./server/derivation.nix { }; mdb-server-no-python = let # this is useful because libpqxx with python support depends on python2.7 # but we don't want python in its closure if we package it into a small @@ -96,11 +97,25 @@ libxml2 = pkgs.libxml2.override { pythonSupport = false; }; }; }; - in (pkgs.python3Packages.callPackage ./server/derivation.nix { }).override { libpqxx = libpqxxWithoutPython; }; + in self'.packages.mdb-server.override { libpqxx = libpqxxWithoutPython; }; mdb-webserver = pkgs.python3Packages.callPackage ./python_client/derivation.nix { }; }; checks = { } // (integrationTests serverVariants); - }; + flake = { + dockerImages = + let + makeDockerImage = name: entrypoint: nixpkgs.legacyPackages.x86_64-linux.dockerTools.buildImage { + inherit name; + tag = "latest"; + config = { Entrypoint = [ entrypoint ]; }; + }; + in + { + mdb-webservice = makeDockerImage "mdb-webservice" "${self.packages.x86_64-linux.mdb-webserver}/bin/webserver"; + mdb-server = makeDockerImage "mdb-server" "${self.packages.x86_64-linux.mdb-server-no-python}/bin/messagedb-server"; + mdb-server-static = makeDockerImage "mdb-server" "${self.packages.x86_64-linux.mdb-server-gcc-9_5_0-boost-1_69_0-static}/bin/messagedb-server"; + }; + }; }; }