Skip to content

Commit 0df084d

Browse files
committed
WIP: static gtk3 apps in C.
Usage: nix-build --no-link survey/default.nix -A pkgsWithStaticHaskellBinaries.meson-tutorial-gtk
1 parent e4a5e74 commit 0df084d

File tree

5 files changed

+165
-1
lines changed

5 files changed

+165
-1
lines changed

meson-tutorial-gtk/gtkmain.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <gtk/gtk.h>
2+
3+
int main(int argc, char **argv) {
4+
GtkWidget *win;
5+
gtk_init(&argc, &argv);
6+
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
7+
gtk_window_set_title(GTK_WINDOW(win), "Hello there");
8+
g_signal_connect(win, "destroy", G_CALLBACK(gtk_main_quit), NULL);
9+
gtk_widget_show(win);
10+
gtk_main();
11+
}

meson-tutorial-gtk/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdio.h>
2+
3+
int main(int argc, char **argv) {
4+
printf("Hello there.\n");
5+
return 0;
6+
}

meson-tutorial-gtk/meson.build

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
project('tutorial', 'c')
2+
# project('tutorial', 'c', default_options: ['c_link_args=-static'])
3+
4+
# executable('demo', 'main.c')
5+
executable('demo', 'main.c', link_args: '-static', install: true)
6+
7+
gtkdep = dependency('gtk+-3.0', static: true)
8+
9+
executable('demo-gtk', 'gtkmain.c', dependencies: gtkdep, install: true, link_args: '-static')

nixpkgs

survey/default.nix

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,9 @@ let
639639

640640
keyutils = previous.keyutils.overrideAttrs (old: { dontDisableStatic = true; });
641641

642+
dbus = previous.dbus.overrideAttrs (old: { dontDisableStatic = true; });
643+
utillinuxMinimal = previous.utillinuxMinimal.overrideAttrs (old: { dontDisableStatic = true; });
644+
642645
libxcb = previous.xorg.libxcb.overrideAttrs (old: { dontDisableStatic = true; });
643646
libX11 = previous.xorg.libX11.overrideAttrs (old: { dontDisableStatic = true; });
644647
libXau = previous.xorg.libXau.overrideAttrs (old: { dontDisableStatic = true; });
@@ -706,6 +709,141 @@ let
706709
# https://git.alpinelinux.org/aports/tree/community/R/APKBUILD?id=e2bce14c748aacb867713cb81a91fad6e8e7f7f6#n56
707710
doCheck = false;
708711
});
712+
713+
gtk3 = previous.gtk3.overrideAttrs (old: {
714+
mesonFlags = (old.mesonFlags or []) ++ [
715+
"-Ddefault_library=both"
716+
#"-Dintrospection=false"
717+
];
718+
});
719+
720+
gtk4 = (previous.gtk3.override {
721+
pango = previous.pango.overrideAttrs (old: rec {
722+
pname = "pango";
723+
version = "1.44.7";
724+
name = "${pname}-${version}";
725+
src = normalPkgs.fetchurl {
726+
url = "mirror://gnome/sources/${pname}/${normalPkgs.lib.versions.majorMinor version}/${name}.tar.xz";
727+
sha256 = "07qvxa2sk90chp1l12han6vxvy098mc37sdqcznyywyv2g6bd9b6";
728+
};
729+
patches = []; # https://gitlab.gnome.org/GNOME/pango/merge_requests/38 was upstreamed
730+
outputs = [ "bin" "dev" "out" ]; # "devdoc" fails to produce with newer pango for unknown reason
731+
});
732+
}).overrideAttrs (old: rec {
733+
# src = final.fetchgit {
734+
src = normalPkgs.fetchgit {
735+
url = "https://gitlab.gnome.org/GNOME/gtk.git";
736+
rev = "ad48bbb8496d2c3b57fcb4367fd85f7664def0c0";
737+
sha256 = "10ng8mmfrril1jf499cxmlvxvk46j7wk4d17i169mjw54fb2hv44";
738+
};
739+
patches = lib.lists.drop 1 old.patches;
740+
741+
propagatedBuildInputs = (old.propagatedBuildInputs or []) ++ [
742+
final.harfbuzz
743+
final.graphene
744+
];
745+
746+
postPatch = ''
747+
files=(
748+
build-aux/meson/post-install.py
749+
demos/gtk-demo/geninclude.py
750+
gdk/broadway/gen-c-array.py
751+
gdk/gen-gdk-gresources-xml.py
752+
gtk/gen-gtk-gresources-xml.py
753+
gtk/gentypefuncs.py
754+
)
755+
756+
chmod +x ''${files[@]}
757+
patchShebangs ''${files[@]}
758+
'';
759+
760+
preConfigure = ''
761+
substituteInPlace gtk/meson.build --replace 'shared_library' 'library'
762+
substituteInPlace testsuite/reftests/meson.build --replace 'shared_library' 'library'
763+
'';
764+
765+
mesonFlags = (old.mesonFlags or []) ++ [
766+
"-Dmedia=none"
767+
"-Dbuild-tests=false" # not needed, just for iteration performance
768+
769+
"-Ddefault_library=both"
770+
];
771+
772+
# Fixes:
773+
# ../demos/gtk-demo/font_features.c: In function ‘add_instance’:
774+
# ../demos/gtk-demo/font_features.c:883:37: error: format not a string literal and no format arguments [-Werror=format-security]
775+
# instance->name = g_strdup_printf (name);
776+
# ^~~~
777+
hardeningDisable = [ "format" ];
778+
779+
preInstall = ''
780+
export PATH="$PWD/gtk/tools:$PATH"
781+
'';
782+
postInstall = lib.optionalString (!final.stdenv.isDarwin) ''
783+
# The updater is needed for nixos env and it's tiny.
784+
moveToOutput gtk/tools/gtk4-update-icon-cache "$out"
785+
# Launcher
786+
moveToOutput gtk/tools/gtk4-launch "$out"
787+
788+
# TODO: patch glib directly
789+
for f in $dev/bin/gtk4-encode-symbolic-svg; do
790+
wrapProgram $f --prefix XDG_DATA_DIRS : "${final.shared-mime-info}/share"
791+
done
792+
'';
793+
794+
pname = "gtk4";
795+
version = "git-2019-11-11";
796+
postFixup = lib.optionalString (!final.stdenv.isDarwin) ''
797+
demos=(gtk4-demo gtk4-demo-application gtk4-icon-browser gtk4-widget-factory)
798+
799+
for program in ''${demos[@]}; do
800+
wrapProgram $dev/bin/$program \
801+
--prefix XDG_DATA_DIRS : "$GSETTINGS_SCHEMAS_PATH:$out/share/gsettings-schemas/${pname}-${version}"
802+
done
803+
'';
804+
805+
});
806+
807+
meson-tutorial-gtk = final.callPackage ({
808+
meson, ninja, pkgconfig, gtk3,
809+
pcre,
810+
harfbuzz,
811+
libpthreadstubs,
812+
libXdmcp,
813+
utillinuxMinimal,
814+
libselinux,
815+
libsepol,
816+
libxkbcommon,
817+
epoxy,
818+
at-spi2-core,
819+
dbus,
820+
libXtst,
821+
}: final.stdenv.mkDerivation {
822+
pname = "meson-tutorial-gtk";
823+
version = "0.0.1";
824+
src = ../meson-tutorial-gtk;
825+
nativeBuildInputs = [ meson pkgconfig ninja ];
826+
buildInputs = [
827+
gtk3
828+
pcre
829+
harfbuzz
830+
libpthreadstubs
831+
libXdmcp
832+
utillinuxMinimal # for libmount
833+
libselinux
834+
libsepol
835+
libxkbcommon
836+
epoxy
837+
at-spi2-core
838+
dbus
839+
libXtst
840+
];
841+
preConfigure = ''
842+
echo
843+
pkg-config --libs --static gtk+-3.0
844+
echo
845+
'';
846+
}) {};
709847
};
710848

711849

0 commit comments

Comments
 (0)