From 522d86406b3ddf7f7c73d928551d187c9a363d54 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= <mail@nh2.me>
Date: Sun, 3 Nov 2019 18:51:12 +0100
Subject: [PATCH 1/9] survey: Fix SDL based exex. Requires nixpkgs update.

This was made possible by making systemd dependencies optional
in recent nixpkgs `master`, initiated by
https://github.com/nh2/static-haskell-nix/issues/50#issuecomment-544675391
with some follow-ups by me at NixCon.
---
 nixpkgs            |  2 +-
 nixpkgs.nix        |  2 +-
 survey/default.nix | 87 ++++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 82 insertions(+), 9 deletions(-)

diff --git a/nixpkgs b/nixpkgs
index 5e6603f..11aa987 160000
--- a/nixpkgs
+++ b/nixpkgs
@@ -1 +1 @@
-Subproject commit 5e6603fbba334804beaad21c9859a7a4347c1242
+Subproject commit 11aa987ea5b5a593c9ca7a38b391804959f905e5
diff --git a/nixpkgs.nix b/nixpkgs.nix
index fa641d6..cfafb35 100644
--- a/nixpkgs.nix
+++ b/nixpkgs.nix
@@ -13,4 +13,4 @@ if builtins.getEnv "STATIC_HASKELL_NIX_CI_NIXPKGS_UNSTABLE_BUILD" == "1"
     if builtins.pathExists ./nixpkgs/pkgs
       then import ./nixpkgs {}
       # Pinned nixpkgs version; should be kept up-to-date with our submodule.
-      else import (fetchTarball https://github.com/nh2/nixpkgs/archive/5e6603fbba334804beaad21c9859a7a4347c1242.tar.gz) {}
+      else import (fetchTarball https://github.com/nh2/nixpkgs/archive/11aa987ea5b5a593c9ca7a38b391804959f905e5.tar.gz) {}
diff --git a/survey/default.nix b/survey/default.nix
index 15354e6..ac894e4 100644
--- a/survey/default.nix
+++ b/survey/default.nix
@@ -618,6 +618,8 @@ let
     libpng = previous.libpng.overrideAttrs (old: { dontDisableStatic = true; });
     libpng_apng = previous.libpng_apng.overrideAttrs (old: { dontDisableStatic = true; });
     libpng12 = previous.libpng12.overrideAttrs (old: { dontDisableStatic = true; });
+    libtiff = previous.libtiff.overrideAttrs (old: { dontDisableStatic = true; });
+    libwebp = previous.libwebp.overrideAttrs (old: { dontDisableStatic = true; });
 
     expat = previous.expat.overrideAttrs (old: { dontDisableStatic = true; });
 
@@ -639,13 +641,25 @@ let
 
     libxcb = previous.xorg.libxcb.overrideAttrs (old: { dontDisableStatic = true; });
     libX11 = previous.xorg.libX11.overrideAttrs (old: { dontDisableStatic = true; });
+    libXau = previous.xorg.libXau.overrideAttrs (old: { dontDisableStatic = true; });
+    libXcursor = previous.xorg.libXcursor.overrideAttrs (old: { dontDisableStatic = true; });
+    libXdmcp = previous.xorg.libXdmcp.overrideAttrs (old: { dontDisableStatic = true; });
     libXext = previous.xorg.libXext.overrideAttrs (old: { dontDisableStatic = true; });
+    libXfixes = previous.xorg.libXfixes.overrideAttrs (old: { dontDisableStatic = true; });
+    libXi = previous.xorg.libXi.overrideAttrs (old: { dontDisableStatic = true; });
     libXinerama = previous.xorg.libXinerama.overrideAttrs (old: { dontDisableStatic = true; });
     libXrandr = previous.xorg.libXrandr.overrideAttrs (old: { dontDisableStatic = true; });
     libXrender = previous.xorg.libXrender.overrideAttrs (old: { dontDisableStatic = true; });
     libXScrnSaver = previous.xorg.libXScrnSaver.overrideAttrs (old: { dontDisableStatic = true; });
-    libXau = previous.xorg.libXau.overrideAttrs (old: { dontDisableStatic = true; });
-    libXdmcp = previous.xorg.libXdmcp.overrideAttrs (old: { dontDisableStatic = true; });
+    libXxf86vm = previous.xorg.libXxf86vm.overrideAttrs (old: { dontDisableStatic = true; });
+
+    SDL2 = previous.SDL2.overrideAttrs (old: { dontDisableStatic = true; });
+    SDL2_gfx = previous.SDL2_gfx.overrideAttrs (old: { dontDisableStatic = true; });
+    SDL2_image = previous.SDL2_image.overrideAttrs (old: { dontDisableStatic = true; });
+    SDL2_mixer = previous.SDL2_mixer.overrideAttrs (old: { dontDisableStatic = true; });
+
+    libjpeg = previous.libjpeg.override (old: { enableStatic = true; });
+    libjpeg_turbo = previous.libjpeg_turbo.override (old: { enableStatic = true; });
 
     openblas = previous.openblas.override { enableStatic = true; };
 
@@ -791,6 +805,13 @@ let
                 enableLibraryProfiling = false;
                 enableExecutableProfiling = false;
 
+                # Skip tests on -O0 because some tests are extremely slow on -O0.
+                # This prevents us from finding upstream correctness issues that
+                # appear only with -O0,
+                # such as https://github.com/bos/double-conversion/issues/26
+                # but that's OK for now as we want -O0 mainly for faster feedback.
+                # doCheck = !disableOptimization;
+
                 # If `disableOptimization` is on for fast iteration, pass `-O0` to GHC.
                 # We use `buildFlags` instead of `configureFlags` so that it's
                 # also in effect for packages which specify e.g.
@@ -1038,6 +1059,63 @@ let
                   [ final.nettle final.bzip2 ]
                   "--libs nettle bz2";
 
+              sdl2-gfx =
+                addStaticLinkerFlagsWithPkgconfig
+                  super.sdl2-gfx
+                  (with final; [
+                    nettle
+                    SDL2
+                    SDL2_gfx
+
+                    libX11
+                    libXext
+                    libXcursor
+                    libXdmcp
+                    libXinerama
+                    libXi
+                    libXrandr
+                    libXxf86vm
+                    libXScrnSaver
+                    libXrender
+                    libXfixes
+                    libXau
+                    libxcb
+                    xorg.libpthreadstubs
+                  ])
+                  "--libs nettle sdl2 SDL2_gfx xcursor";
+
+              sdl2-image =
+                addStaticLinkerFlagsWithPkgconfig
+                  super.sdl2-image
+                  (with final; [
+                    nettle
+                    SDL2
+                    SDL2_image
+
+                    libX11
+                    libXext
+                    libXcursor
+                    libXdmcp
+                    libXinerama
+                    libXi
+                    libXrandr
+                    libXxf86vm
+                    libXScrnSaver
+                    libXrender
+                    libXfixes
+                    libXau
+                    libxcb
+                    xorg.libpthreadstubs
+
+                    libjpeg
+                    libpng
+                    libtiff
+                    zlib_both
+                    lzma
+                    libwebp
+                  ])
+                  "--libs nettle sdl2 SDL2_image xcursor libpng libjpeg libtiff-4 libwebp";
+
               # Added for #14
               tttool = callCabal2nix "tttool" (final.fetchFromGitHub {
                 owner = "entropia";
@@ -1247,11 +1325,6 @@ in
         "OpenAL" # transitively depends on `systemd`, which doesn't build with musl
         "qchas" # openmp linker error via openblas
         "rhine-gloss" # needs opengl
-        "sdl2" # transitively depends on `systemd`, which doesn't build with musl
-        "sdl2-gfx" # see `sdl2`
-        "sdl2-image" # see `sdl2`
-        "sdl2-mixer" # see `sdl2`
-        "sdl2-ttf" # see `sdl2`
         "soxlib" # transitively depends on `systemd`, which doesn't build with musl
         "yesod-paginator" # some `curl` build failure; seems to be in *fetching* the source .tar.gz in `fetchurl`, and gss is enabled there even though we tried to disable it
       ];

From e4a5e74c0ad5d9d114b7fbb7b73a849badb8c242 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= <mail@nh2.me>
Date: Sun, 3 Nov 2019 19:37:26 +0100
Subject: [PATCH 2/9] survey: Reduce excluded stackage executables.

With the recent update to nixpkgs master and my PRs to make systemd
optional, we have a lot less failing packages.
---
 survey/default.nix | 43 +++++++++++++++++++++----------------------
 1 file changed, 21 insertions(+), 22 deletions(-)

diff --git a/survey/default.nix b/survey/default.nix
index ac894e4..7349f87 100644
--- a/survey/default.nix
+++ b/survey/default.nix
@@ -959,6 +959,11 @@ let
               #     focuslist-doctests: focuslist-doctests: unable to load package `ghc-prim-0.5.3'
               focuslist = dontCheck super.focuslist;
 
+              # Fails in doctests with:
+              #     doctests: /nix/store/nda51m9gymbx9qvzmjpfd4393jqq0gdm-ghc-8.6.5/lib/ghc-8.6.5/ghc-prim-0.5.3/HSghc-prim-0.5.3.o: unknown symbol `exp'
+              #     doctests: doctests: unable to load package `ghc-prim-0.5.3'
+              yesod-paginator = dontCheck super.yesod-paginator;
+
               # Disabling test suite because it takes extremely long (> 30 minutes):
               # https://github.com/mrkkrp/zip/issues/55
               zip = dontCheck super.zip;
@@ -1301,32 +1306,26 @@ in
       builtins.removeAttrs allStackageExecutables [
         # List of executables that don't work for reasons not yet investigated.
         # When changing this file, we should always check if this list grows or shrinks.
-        "Agda"
-        "Allure"
-        "ALUT"
-        "clash-ghc"
-        "csg"
-        "cuda" # transitively depends on `systemd`, which doesn't build with musl
-        "debug"
-        "diagrams-builder"
-        "ersatz"
-        "gloss-examples" # needs opengl
-        "gtk3" # problem compiling `glib` dependency with `Distribution.Simple.UserHooks.UserHooks` type mismatch across Cabal versions; should go away once we no longer have to patch Cabal
+        "Agda" # anonymous function at build-support/fetchurl/boot.nix:5:1 called with unexpected argument 'meta', at build-support/fetchpatch/default.nix:14:1
+        "Allure" # marked as broken
+        "csg" # marked as broken
+        "cuda" # needs `allowUnfree = true`; enabling it gives `unsupported platform for the pure Linux stdenv`
+        "debug" # marked as broken
+        "diagrams-builder" # marked as broken
+        "ersatz" # marked as broken
+        "gloss-examples" # needs opengl: `cannot find -lGLU` `-lGL`
+        "gtk3" # problem compiling `glib` dependency: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a PIE object
         "H" # `zgemm_: symbol not found` when compiling Main; not clear how that can be provided
         "hamilton" # openmp linker error via openblas
-        "hquantlib"
-        "ihaskell"
-        "jack" # transitively depends on `systemd`, which doesn't build with musl
-        "LambdaHack"
+        "hquantlib" # marked as broken
+        "ihaskell" # marked as broken
+        "LambdaHack" # marked as broken
         "language-puppet" # dependency `hruby` does not build
-        "learn-physics"
-        "leveldb-haskell"
-        "odbc" # undeclared `<odbcss.h>` dependency
-        "OpenAL" # transitively depends on `systemd`, which doesn't build with musl
+        "learn-physics" # needs opengl: `cannot find -lGLU` `-lGL`
+        "odbc" # marked as broken
         "qchas" # openmp linker error via openblas
-        "rhine-gloss" # needs opengl
-        "soxlib" # transitively depends on `systemd`, which doesn't build with musl
-        "yesod-paginator" # some `curl` build failure; seems to be in *fetching* the source .tar.gz in `fetchurl`, and gss is enabled there even though we tried to disable it
+        "rhine-gloss" # needs opengl: `cannot find -lGLU` `-lGL`
+        "soxlib" # dependency `sox` fails with: `formats.c:425:4: error: #error FIX NEEDED HERE`
       ];
 
     inherit normalPkgs;

From 820f547e3d421808f89bce7e982601a720634438 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= <mail@nh2.me>
Date: Mon, 11 Nov 2019 21:43:14 +0100
Subject: [PATCH 3/9] WIP: static gtk3 apps in C.

Usage:

    nix-build --no-link survey/default.nix -A pkgsWithStaticHaskellBinaries.meson-tutorial-gtk
---
 meson-tutorial-gtk/gtkmain.c   |  11 +++
 meson-tutorial-gtk/main.c      |   6 ++
 meson-tutorial-gtk/meson.build |   9 +++
 nixpkgs                        |   2 +-
 survey/default.nix             | 138 +++++++++++++++++++++++++++++++++
 5 files changed, 165 insertions(+), 1 deletion(-)
 create mode 100644 meson-tutorial-gtk/gtkmain.c
 create mode 100644 meson-tutorial-gtk/main.c
 create mode 100644 meson-tutorial-gtk/meson.build

diff --git a/meson-tutorial-gtk/gtkmain.c b/meson-tutorial-gtk/gtkmain.c
new file mode 100644
index 0000000..be5abe7
--- /dev/null
+++ b/meson-tutorial-gtk/gtkmain.c
@@ -0,0 +1,11 @@
+#include <gtk/gtk.h>
+
+int main(int argc, char **argv) {
+  GtkWidget *win;
+  gtk_init(&argc, &argv);
+  win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+  gtk_window_set_title(GTK_WINDOW(win), "Hello there");
+  g_signal_connect(win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
+  gtk_widget_show(win);
+  gtk_main();
+}
diff --git a/meson-tutorial-gtk/main.c b/meson-tutorial-gtk/main.c
new file mode 100644
index 0000000..0b590d1
--- /dev/null
+++ b/meson-tutorial-gtk/main.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+  printf("Hello there.\n");
+  return 0;
+}
diff --git a/meson-tutorial-gtk/meson.build b/meson-tutorial-gtk/meson.build
new file mode 100644
index 0000000..e695dc6
--- /dev/null
+++ b/meson-tutorial-gtk/meson.build
@@ -0,0 +1,9 @@
+project('tutorial', 'c')
+# project('tutorial', 'c', default_options: ['c_link_args=-static'])
+
+# executable('demo', 'main.c')
+executable('demo', 'main.c', link_args: '-static', install: true)
+
+gtkdep = dependency('gtk+-3.0', static: true)
+
+executable('demo-gtk', 'gtkmain.c', dependencies: gtkdep, install: true, link_args: '-static')
diff --git a/nixpkgs b/nixpkgs
index 11aa987..165420d 160000
--- a/nixpkgs
+++ b/nixpkgs
@@ -1 +1 @@
-Subproject commit 11aa987ea5b5a593c9ca7a38b391804959f905e5
+Subproject commit 165420dcc4b43e34666a4285d42e70fefe228561
diff --git a/survey/default.nix b/survey/default.nix
index 7349f87..a86fdc3 100644
--- a/survey/default.nix
+++ b/survey/default.nix
@@ -639,6 +639,9 @@ let
 
     keyutils = previous.keyutils.overrideAttrs (old: { dontDisableStatic = true; });
 
+    dbus = previous.dbus.overrideAttrs (old: { dontDisableStatic = true; });
+    utillinuxMinimal = previous.utillinuxMinimal.overrideAttrs (old: { dontDisableStatic = true; });
+
     libxcb = previous.xorg.libxcb.overrideAttrs (old: { dontDisableStatic = true; });
     libX11 = previous.xorg.libX11.overrideAttrs (old: { dontDisableStatic = true; });
     libXau = previous.xorg.libXau.overrideAttrs (old: { dontDisableStatic = true; });
@@ -706,6 +709,141 @@ let
       # https://git.alpinelinux.org/aports/tree/community/R/APKBUILD?id=e2bce14c748aacb867713cb81a91fad6e8e7f7f6#n56
       doCheck = false;
     });
+
+    gtk3 = previous.gtk3.overrideAttrs (old: {
+      mesonFlags = (old.mesonFlags or []) ++ [
+        "-Ddefault_library=both"
+        #"-Dintrospection=false"
+      ];
+    });
+
+    gtk4 = (previous.gtk3.override {
+      pango = previous.pango.overrideAttrs (old: rec {
+        pname = "pango";
+        version = "1.44.7";
+        name = "${pname}-${version}";
+        src = normalPkgs.fetchurl {
+          url = "mirror://gnome/sources/${pname}/${normalPkgs.lib.versions.majorMinor version}/${name}.tar.xz";
+          sha256 = "07qvxa2sk90chp1l12han6vxvy098mc37sdqcznyywyv2g6bd9b6";
+        };
+        patches = []; # https://gitlab.gnome.org/GNOME/pango/merge_requests/38 was upstreamed
+        outputs = [ "bin" "dev" "out" ]; # "devdoc" fails to produce with newer pango for unknown reason
+      });
+    }).overrideAttrs (old: rec {
+      # src = final.fetchgit {
+      src = normalPkgs.fetchgit {
+        url = "https://gitlab.gnome.org/GNOME/gtk.git";
+        rev = "ad48bbb8496d2c3b57fcb4367fd85f7664def0c0";
+        sha256 = "10ng8mmfrril1jf499cxmlvxvk46j7wk4d17i169mjw54fb2hv44";
+      };
+      patches = lib.lists.drop 1 old.patches;
+
+      propagatedBuildInputs = (old.propagatedBuildInputs or []) ++ [
+        final.harfbuzz
+        final.graphene
+      ];
+
+      postPatch = ''
+        files=(
+          build-aux/meson/post-install.py
+          demos/gtk-demo/geninclude.py
+          gdk/broadway/gen-c-array.py
+          gdk/gen-gdk-gresources-xml.py
+          gtk/gen-gtk-gresources-xml.py
+          gtk/gentypefuncs.py
+        )
+
+        chmod +x ''${files[@]}
+        patchShebangs ''${files[@]}
+      '';
+
+      preConfigure = ''
+        substituteInPlace gtk/meson.build --replace 'shared_library' 'library'
+        substituteInPlace testsuite/reftests/meson.build --replace 'shared_library' 'library'
+      '';
+
+      mesonFlags = (old.mesonFlags or []) ++ [
+        "-Dmedia=none"
+        "-Dbuild-tests=false" # not needed, just for iteration performance
+
+        "-Ddefault_library=both"
+      ];
+
+      # Fixes:
+      #     ../demos/gtk-demo/font_features.c: In function ‘add_instance’:
+      #     ../demos/gtk-demo/font_features.c:883:37: error: format not a string literal and no format arguments [-Werror=format-security]
+      #        instance->name = g_strdup_printf (name);
+      #                                          ^~~~
+      hardeningDisable = [ "format" ];
+
+      preInstall = ''
+        export PATH="$PWD/gtk/tools:$PATH"
+      '';
+      postInstall = lib.optionalString (!final.stdenv.isDarwin) ''
+        # The updater is needed for nixos env and it's tiny.
+        moveToOutput gtk/tools/gtk4-update-icon-cache "$out"
+        # Launcher
+        moveToOutput gtk/tools/gtk4-launch "$out"
+
+        # TODO: patch glib directly
+        for f in $dev/bin/gtk4-encode-symbolic-svg; do
+          wrapProgram $f --prefix XDG_DATA_DIRS : "${final.shared-mime-info}/share"
+        done
+      '';
+
+      pname = "gtk4";
+      version = "git-2019-11-11";
+      postFixup =  lib.optionalString (!final.stdenv.isDarwin) ''
+        demos=(gtk4-demo gtk4-demo-application gtk4-icon-browser gtk4-widget-factory)
+
+        for program in ''${demos[@]}; do
+          wrapProgram $dev/bin/$program \
+            --prefix XDG_DATA_DIRS : "$GSETTINGS_SCHEMAS_PATH:$out/share/gsettings-schemas/${pname}-${version}"
+        done
+      '';
+
+    });
+
+    meson-tutorial-gtk = final.callPackage ({
+        meson, ninja, pkgconfig, gtk3,
+        pcre,
+        harfbuzz,
+        libpthreadstubs,
+        libXdmcp,
+        utillinuxMinimal,
+        libselinux,
+        libsepol,
+        libxkbcommon,
+        epoxy,
+        at-spi2-core,
+        dbus,
+        libXtst,
+      }: final.stdenv.mkDerivation {
+      pname = "meson-tutorial-gtk";
+      version = "0.0.1";
+      src = ../meson-tutorial-gtk;
+      nativeBuildInputs = [ meson pkgconfig ninja ];
+      buildInputs = [
+        gtk3
+        pcre
+        harfbuzz
+        libpthreadstubs
+        libXdmcp
+        utillinuxMinimal # for libmount
+        libselinux
+        libsepol
+        libxkbcommon
+        epoxy
+        at-spi2-core
+        dbus
+        libXtst
+      ];
+      preConfigure = ''
+        echo
+        pkg-config --libs --static gtk+-3.0
+        echo
+      '';
+    }) {};
   };
 
 

From 4279149a3b182c0b4337a3e0f34f13c336611cb6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= <mail@nh2.me>
Date: Mon, 25 Nov 2019 01:19:45 +0100
Subject: [PATCH 4/9] More WIP

---
 survey/default.nix | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/survey/default.nix b/survey/default.nix
index a86fdc3..d86e194 100644
--- a/survey/default.nix
+++ b/survey/default.nix
@@ -648,6 +648,7 @@ let
     libXcursor = previous.xorg.libXcursor.overrideAttrs (old: { dontDisableStatic = true; });
     libXdmcp = previous.xorg.libXdmcp.overrideAttrs (old: { dontDisableStatic = true; });
     libXext = previous.xorg.libXext.overrideAttrs (old: { dontDisableStatic = true; });
+    libXtst = previous.xorg.libXtst.overrideAttrs (old: { dontDisableStatic = true; });
     libXfixes = previous.xorg.libXfixes.overrideAttrs (old: { dontDisableStatic = true; });
     libXi = previous.xorg.libXi.overrideAttrs (old: { dontDisableStatic = true; });
     libXinerama = previous.xorg.libXinerama.overrideAttrs (old: { dontDisableStatic = true; });
@@ -806,7 +807,8 @@ let
 
     meson-tutorial-gtk = final.callPackage ({
         meson, ninja, pkgconfig, gtk3,
-        pcre,
+        pcre_static,
+        zlib_both,
         harfbuzz,
         libpthreadstubs,
         libXdmcp,
@@ -825,7 +827,8 @@ let
       nativeBuildInputs = [ meson pkgconfig ninja ];
       buildInputs = [
         gtk3
-        pcre
+        pcre_static
+        zlib_both
         harfbuzz
         libpthreadstubs
         libXdmcp
@@ -1280,6 +1283,13 @@ let
               #      Most likely it is because the `libX*` packages are available once on the top-level
               #      namespace (where we override them), and once under `xorg.libX*`, where we don't
               #      override them; it seems that `X11` depends on the latter.
+              # Note that the addition of `xorg.*` packages to the global
+              # package set available to derivation (`callPackage`) arguments
+              # is set up here:
+              #     https://github.com/NixOS/nixpkgs/blob/9a2c7caa43f1cb83b3efd156de35aea85196f32f/pkgs/top-level/splice.nix#L125-L132
+              # According to `clever`, the right place to override them should
+              # be inside `xorg` and then the top-level ones should be
+              # overridden automatically.
               X11 = super.X11.override {
                 libX11 = final.libX11;
                 libXext = final.libXext;
@@ -1287,7 +1297,19 @@ let
                 libXrandr = final.libXrandr;
                 libXrender = final.libXrender;
                 libXScrnSaver = final.libXScrnSaver;
+
               };
+              xorg = super.xorg.override (old: {
+                libX11 = final.libX11;
+                libXext = final.libXext;
+                libXinerama = final.libXinerama;
+                libXrandr = final.libXrandr;
+                libXrender = final.libXrender;
+                libXScrnSaver = final.libXScrnSaver;
+
+                libXtst = final.libXtst;
+              });
+
 
               # Note that xmonad links, but it doesn't run, because it tries to open
               # `libgmp.so.3` at run time.

From 76471855a7dbd452f51d8f411d978a9f463abccc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= <mail@nh2.me>
Date: Mon, 23 Nov 2020 18:50:31 +0100
Subject: [PATCH 5/9] WIP: More gtk building

We need GL support because of gdk -> epoxy -> GL.
---
 survey/default.nix | 96 ++++++++++++++++++++++++++++++----------------
 1 file changed, 62 insertions(+), 34 deletions(-)

diff --git a/survey/default.nix b/survey/default.nix
index d86e194..d690944 100644
--- a/survey/default.nix
+++ b/survey/default.nix
@@ -614,7 +614,12 @@ let
         "--enable-static"
       ];
     });
-    cairo = previous.cairo.overrideAttrs (old: { dontDisableStatic = true; });
+    cairo = (previous.cairo.overrideAttrs (old: { dontDisableStatic = true; })).override {
+      # Disabling OpenGL support for now because I don't know if statically
+      # linking it is possible (it may depend on the hardware).
+      libGLSupported = false;
+      glSupport = false;
+    };
     libpng = previous.libpng.overrideAttrs (old: { dontDisableStatic = true; });
     libpng_apng = previous.libpng_apng.overrideAttrs (old: { dontDisableStatic = true; });
     libpng12 = previous.libpng12.overrideAttrs (old: { dontDisableStatic = true; });
@@ -642,20 +647,45 @@ let
     dbus = previous.dbus.overrideAttrs (old: { dontDisableStatic = true; });
     utillinuxMinimal = previous.utillinuxMinimal.overrideAttrs (old: { dontDisableStatic = true; });
 
-    libxcb = previous.xorg.libxcb.overrideAttrs (old: { dontDisableStatic = true; });
-    libX11 = previous.xorg.libX11.overrideAttrs (old: { dontDisableStatic = true; });
-    libXau = previous.xorg.libXau.overrideAttrs (old: { dontDisableStatic = true; });
-    libXcursor = previous.xorg.libXcursor.overrideAttrs (old: { dontDisableStatic = true; });
-    libXdmcp = previous.xorg.libXdmcp.overrideAttrs (old: { dontDisableStatic = true; });
-    libXext = previous.xorg.libXext.overrideAttrs (old: { dontDisableStatic = true; });
-    libXtst = previous.xorg.libXtst.overrideAttrs (old: { dontDisableStatic = true; });
-    libXfixes = previous.xorg.libXfixes.overrideAttrs (old: { dontDisableStatic = true; });
-    libXi = previous.xorg.libXi.overrideAttrs (old: { dontDisableStatic = true; });
-    libXinerama = previous.xorg.libXinerama.overrideAttrs (old: { dontDisableStatic = true; });
-    libXrandr = previous.xorg.libXrandr.overrideAttrs (old: { dontDisableStatic = true; });
-    libXrender = previous.xorg.libXrender.overrideAttrs (old: { dontDisableStatic = true; });
-    libXScrnSaver = previous.xorg.libXScrnSaver.overrideAttrs (old: { dontDisableStatic = true; });
-    libXxf86vm = previous.xorg.libXxf86vm.overrideAttrs (old: { dontDisableStatic = true; });
+    # Note that the addition of `xorg.*` packages to the global
+    # package set available to derivation (`callPackage`) arguments
+    # is set up here:
+    #     https://github.com/NixOS/nixpkgs/blob/9a2c7caa43f1cb83b3efd156de35aea85196f32f/pkgs/top-level/splice.nix#L125-L132
+    # According to `clever`, the right place to override them should
+    # be inside `xorg` and then the top-level ones should be
+    # overridden automatically.
+    #
+    # Btw, creating overridable scopes works like this:
+    #     https://github.com/cleverca22/nix-tests/blob/22a32a1c43162817dba0cd9dd6f2b35590582e63/kexec/simple-test.nix#L52
+    xorg = previous.xorg.overrideScope' (final_xorg: previous_xorg:
+      lib.mapAttrs
+        (name: value: value.overrideAttrs (old: { dontDisableStatic = true; }))
+        previous_xorg
+    );
+    epoxy = previous.epoxy.override {
+      enableStatic = true;
+      enableEgl = false;
+    };
+    graphite2 = previous.graphite2.override { enableStatic = true; };
+    harfbuzz = previous.harfbuzz.override { enableStatic = true; };
+    wayland = previous.wayland.override { enableStatic = true; };
+
+    at-spi2-atk = previous.at-spi2-atk.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=both" ]; });
+    at-spi2-core = previous.at-spi2-core.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=both" ]; });
+    atk = previous.atk.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=both" ]; });
+    fribidi = previous.fribidi.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=both" ]; });
+    gdk-pixbuf = previous.gdk-pixbuf.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=both" ]; });
+    glib = previous.glib.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=both" ]; });
+    libxkbcommon = previous.libxkbcommon.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=both" ]; });
+    pango = previous.pango.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=both" ]; });
+    # mesa_glu = previous.mesa_glu.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=both" ]; });
+    libglvnd = previous.libglvnd.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=both" ]; });
+
+    # Changing `shared_library` -> `library` and using `-Ddefault_library=both`
+    # in mesa did not work for me (still to investigate why), but doing the
+    # same with `-Ddefault_library=static` worked.
+    # mesa = previous.mesa.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=both" ]; });
+    mesa = previous.mesa.overrideAttrs (old: { mesonFlags = (old.mesonFlags or []) ++ [ "-Ddefault_library=static" ]; });
 
     SDL2 = previous.SDL2.overrideAttrs (old: { dontDisableStatic = true; });
     SDL2_gfx = previous.SDL2_gfx.overrideAttrs (old: { dontDisableStatic = true; });
@@ -669,6 +699,10 @@ let
 
     openssl = previous.openssl.override { static = true; };
 
+    # Disabling kerberos support for now, as openssh's `./configure` fails to
+    # detect its functions due to linker error, so the build breaks, see #68.
+    openssh = previous.openssh.override { withKerberos = false; };
+
     krb5 = previous.krb5.override {
       # Note [krb5 can only be static XOR shared]
       # krb5 does not support building both static and shared at the same time.
@@ -711,11 +745,16 @@ let
       doCheck = false;
     });
 
-    gtk3 = previous.gtk3.overrideAttrs (old: {
+    gtk3 = (previous.gtk3.overrideAttrs (old: {
       mesonFlags = (old.mesonFlags or []) ++ [
+        # Just `static` doesn't currently work, the `g-ir-scanner` fails during gtk's build then.
         "-Ddefault_library=both"
         #"-Dintrospection=false"
       ];
+    })).override (old_gtk3: {
+      # Wayland requires EGL support for which we have not figured out yet
+      # whether it can be statically linked.
+      waylandSupport = false;
     });
 
     gtk4 = (previous.gtk3.override {
@@ -809,6 +848,7 @@ let
         meson, ninja, pkgconfig, gtk3,
         pcre_static,
         zlib_both,
+        bzip2_static,
         harfbuzz,
         libpthreadstubs,
         libXdmcp,
@@ -820,6 +860,9 @@ let
         at-spi2-core,
         dbus,
         libXtst,
+        libGL,
+        mesa,
+        binutils,
       }: final.stdenv.mkDerivation {
       pname = "meson-tutorial-gtk";
       version = "0.0.1";
@@ -829,6 +872,7 @@ let
         gtk3
         pcre_static
         zlib_both
+        bzip2_static
         harfbuzz
         libpthreadstubs
         libXdmcp
@@ -840,6 +884,8 @@ let
         at-spi2-core
         dbus
         libXtst
+        libGL
+        mesa
       ];
       preConfigure = ''
         echo
@@ -1283,13 +1329,6 @@ let
               #      Most likely it is because the `libX*` packages are available once on the top-level
               #      namespace (where we override them), and once under `xorg.libX*`, where we don't
               #      override them; it seems that `X11` depends on the latter.
-              # Note that the addition of `xorg.*` packages to the global
-              # package set available to derivation (`callPackage`) arguments
-              # is set up here:
-              #     https://github.com/NixOS/nixpkgs/blob/9a2c7caa43f1cb83b3efd156de35aea85196f32f/pkgs/top-level/splice.nix#L125-L132
-              # According to `clever`, the right place to override them should
-              # be inside `xorg` and then the top-level ones should be
-              # overridden automatically.
               X11 = super.X11.override {
                 libX11 = final.libX11;
                 libXext = final.libXext;
@@ -1297,18 +1336,7 @@ let
                 libXrandr = final.libXrandr;
                 libXrender = final.libXrender;
                 libXScrnSaver = final.libXScrnSaver;
-
               };
-              xorg = super.xorg.override (old: {
-                libX11 = final.libX11;
-                libXext = final.libXext;
-                libXinerama = final.libXinerama;
-                libXrandr = final.libXrandr;
-                libXrender = final.libXrender;
-                libXScrnSaver = final.libXScrnSaver;
-
-                libXtst = final.libXtst;
-              });
 
 
               # Note that xmonad links, but it doesn't run, because it tries to open

From 19a1b4acd6b022b9566c3ab27768359f3e635af0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= <mail@nh2.me>
Date: Mon, 23 Nov 2020 19:57:46 +0100
Subject: [PATCH 6/9] meson-tutorial-gtk: Add a button with click handler

---
 meson-tutorial-gtk/gtkmain.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/meson-tutorial-gtk/gtkmain.c b/meson-tutorial-gtk/gtkmain.c
index be5abe7..22a78e3 100644
--- a/meson-tutorial-gtk/gtkmain.c
+++ b/meson-tutorial-gtk/gtkmain.c
@@ -1,11 +1,22 @@
 #include <gtk/gtk.h>
 
+void button_event(GtkWidget *widget, gpointer *data) {
+  g_print("Button clicked\n");
+  gtk_button_set_label(GTK_BUTTON(widget), "It worked!");
+}
+
 int main(int argc, char **argv) {
   GtkWidget *win;
   gtk_init(&argc, &argv);
   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title(GTK_WINDOW(win), "Hello there");
   g_signal_connect(win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
+
+  GtkWidget *button = gtk_button_new_with_mnemonic("_Click me!");
+  gtk_widget_show(button);
+  g_signal_connect(button, "pressed", G_CALLBACK(button_event), NULL);
+  gtk_container_add(GTK_CONTAINER(win), button);
+
   gtk_widget_show(win);
   gtk_main();
 }

From 27511eef91927896a9ede5db3f1e8a1df2b762f2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= <mail@nh2.me>
Date: Mon, 23 Nov 2020 22:19:40 +0100
Subject: [PATCH 7/9] static gtk: Add failing glade example.

See https://github.com/nh2/static-haskell-nix/issues/50#issuecomment-732405876

It currently fails with:

    (demo-glade:8872): GModule-CRITICAL **: 22:19:02.953: g_module_symbol: assertion 'module != NULL' failed

    (demo-glade:8872): GModule-CRITICAL **: 22:19:02.986: g_module_close: assertion 'module != NULL' failed
    Dynamic loading not supported
    Failed to load module: /nix/store/88gpkpcfjbgihn3fl8b8vk5ggfs8wn73-dconf-0.36.0-lib/lib/gio/modules/libdconfsettings.so
    Dynamic loading not supported
    Failed to load module: /nix/store/d6l7xwbdm23xgds5vafzibw57790zw71-glib-networking-2.64.3/lib/gio/modules/libgiolibproxy.so
    Dynamic loading not supported
    Failed to load module: /nix/store/d6l7xwbdm23xgds5vafzibw57790zw71-glib-networking-2.64.3/lib/gio/modules/libgiognutls.so
    Dynamic loading not supported
    Failed to load module: /nix/store/d6l7xwbdm23xgds5vafzibw57790zw71-glib-networking-2.64.3/lib/gio/modules/libgiognomeproxy.so
    Dynamic loading not supported
    Failed to load module: /nix/store/bkjpypri81svkgq5rdfd4mdn33ic1pja-gvfs-1.44.1/lib/gio/modules/libgioremote-volume-monitor.so
    Dynamic loading not supported
    Failed to load module: /nix/store/bkjpypri81svkgq5rdfd4mdn33ic1pja-gvfs-1.44.1/lib/gio/modules/libgvfsdbus.so

    (demo-glade:8872): GModule-CRITICAL **: 22:19:03.183: g_module_symbol: assertion 'module != NULL' failed

    (demo-glade:8872): Gtk-ERROR **: 22:19:03.186: gtk_builder_connect_signals() requires working GModule
---
 meson-tutorial-gtk/glade-example-main.c | 52 ++++++++++++++++++++
 meson-tutorial-gtk/glade-example.glade  | 63 +++++++++++++++++++++++++
 meson-tutorial-gtk/meson.build          |  2 +
 3 files changed, 117 insertions(+)
 create mode 100644 meson-tutorial-gtk/glade-example-main.c
 create mode 100644 meson-tutorial-gtk/glade-example.glade

diff --git a/meson-tutorial-gtk/glade-example-main.c b/meson-tutorial-gtk/glade-example-main.c
new file mode 100644
index 0000000..6cf30b3
--- /dev/null
+++ b/meson-tutorial-gtk/glade-example-main.c
@@ -0,0 +1,52 @@
+#include <gtk/gtk.h>
+
+// Roughly following https://prognotes.net/2016/03/gtk-3-c-code-hello-world-tutorial-using-glade-3/
+
+GtkWidget *g_label_hello;
+GtkWidget *g_button_count;
+
+void on_button_hello_clicked()
+{
+  static unsigned int count = 0;
+  char str_count[30] = {0};
+
+  count++;
+  snprintf(str_count, 30, "%d", count);
+  gtk_label_set_text(GTK_LABEL(g_label_hello), str_count);
+}
+
+// called when window is closed
+void on_window_main_destroy()
+{
+  gtk_main_quit();
+}
+
+int main(int argc, char **argv)
+{
+  GtkBuilder *builder;
+  GtkWidget *window;
+
+  gtk_init(&argc, &argv);
+
+  builder = gtk_builder_new();
+  GError *error = NULL;
+  if (0 == gtk_builder_add_from_file(builder, "glade-example.glade", &error))
+  {
+    g_printerr("Error loading file: %s\n", error->message);
+    g_clear_error(&error);
+    return 1;
+  }
+
+  window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
+  gtk_builder_connect_signals(builder, NULL);
+
+  // get pointers to the two labels
+  g_label_hello = GTK_WIDGET(gtk_builder_get_object(builder, "label_hello"));
+
+  g_object_unref(builder);
+
+  gtk_widget_show(window);
+  gtk_main();
+
+  return 0;
+}
diff --git a/meson-tutorial-gtk/glade-example.glade b/meson-tutorial-gtk/glade-example.glade
new file mode 100644
index 0000000..31debc4
--- /dev/null
+++ b/meson-tutorial-gtk/glade-example.glade
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.36.0 -->
+<interface>
+  <requires lib="gtk+" version="3.22"/>
+  <object class="GtkWindow">
+    <property name="name">window_main</property>
+    <property name="can_focus">False</property>
+    <child>
+      <object class="GtkGrid">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <child>
+          <object class="GtkButton" id="button_hello">
+            <property name="label" translatable="yes">button</property>
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">True</property>
+            <signal name="clicked" handler="on_button_hello_clicked" swapped="no"/>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkLabel" id="label_hello">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="label" translatable="yes">label</property>
+          </object>
+          <packing>
+            <property name="left_attach">1</property>
+            <property name="top_attach">0</property>
+          </packing>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+      </object>
+    </child>
+    <child type="titlebar">
+      <placeholder/>
+    </child>
+  </object>
+</interface>
diff --git a/meson-tutorial-gtk/meson.build b/meson-tutorial-gtk/meson.build
index e695dc6..80b912b 100644
--- a/meson-tutorial-gtk/meson.build
+++ b/meson-tutorial-gtk/meson.build
@@ -7,3 +7,5 @@ executable('demo', 'main.c', link_args: '-static', install: true)
 gtkdep = dependency('gtk+-3.0', static: true)
 
 executable('demo-gtk', 'gtkmain.c', dependencies: gtkdep, install: true, link_args: '-static')
+
+executable('demo-glade', 'glade-example-main.c', dependencies: gtkdep, install: true, link_args: '-static')

From 0b12750b351aac0f192d54ee4b7f28297bf6c51c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= <mail@nh2.me>
Date: Sat, 8 Jun 2024 18:13:13 +0200
Subject: [PATCH 8/9] WIP: Saving some changes here after some years. TODO:
 Review

---
 nixpkgs            |  2 +-
 survey/default.nix | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/nixpkgs b/nixpkgs
index 165420d..6ec5829 160000
--- a/nixpkgs
+++ b/nixpkgs
@@ -1 +1 @@
-Subproject commit 165420dcc4b43e34666a4285d42e70fefe228561
+Subproject commit 6ec5829df36676d155e22d3915f42bb5796043db
diff --git a/survey/default.nix b/survey/default.nix
index d690944..d899a05 100644
--- a/survey/default.nix
+++ b/survey/default.nix
@@ -711,6 +711,14 @@ let
       staticOnly = true;
     };
 
+    samba4 = previous.samba4.override {
+      # We haven't figured out how to build samba with Kerberos yet,
+      # getting the error:
+      #     Checking for gss_display_status                                                                 : not found
+      #     ERROR: WAF build with MIT Krb5 requires working GSSAPI implementation
+      enableKerberos = false;
+    };
+
     # See comments on `statify_curl_including_exe` for the interaction with krb5!
     # As mentioned in [Packages that can't be overridden by overlays], we can't
     # override zlib to have static libs, so we have to pass in `zlib_both` explicitly
@@ -863,6 +871,7 @@ let
         libGL,
         mesa,
         binutils,
+        gvfs, # for loading glade files
       }: final.stdenv.mkDerivation {
       pname = "meson-tutorial-gtk";
       version = "0.0.1";
@@ -886,6 +895,7 @@ let
         libXtst
         libGL
         mesa
+        gvfs
       ];
       preConfigure = ''
         echo

From 97169ebc4d884f4042a27d81f0ee1764117c83da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= <mail@nh2.me>
Date: Sat, 8 Jun 2024 18:37:39 +0200
Subject: [PATCH 9/9] WIP: Update nixpkgs submodule with WIP changes

---
 nixpkgs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/nixpkgs b/nixpkgs
index 6ec5829..112769e 160000
--- a/nixpkgs
+++ b/nixpkgs
@@ -1 +1 @@
-Subproject commit 6ec5829df36676d155e22d3915f42bb5796043db
+Subproject commit 112769e0c8ad9a39cb0385b8ca1676363de6ee4e