Setup environment for gtkrs on NixOS

Hi everyone!
I want to develop using the Gtk4 libraries and I currently have a dedicated configuration in /etc/nixos/develop-gtk.nix

{ pkgs, ... }:

{
  environment.systemPackages = with pkgs; [
    desktop-file-utils
    gcc
    gtk4
    libadwaita
    meson
    pkg-config
    rustup
  ];
}

So I started a project following the official guide

cargo new my-gtk-app
cd my-gtk-app

But when I look for the development version

[antonio@nixos:~]$ pkg-config --modversion gtk4
Package gtk4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk4' found

I found the folder containing the gtk4.pc file

[antonio@nixos:~]$ dirname "$(find "$(ls -1d /nix/store/*gtk4*dev)" -iname "*gtk4.pc")"
/nix/store/nv8x7438mp3gdd0wdpnc18nm57jxmwah-gtk4-4.22.4-dev/lib/pkgconfig

Then I exported the path into PKG_CONFIG_PATH and the command now works.

[antonio@nixos:~]$ pkg-config --modversion gtk4
4.22.4

I read that I can use nix-shell, but I don’t want a temporary solution.

Is there a way to tell pkg-config to work properly using my /etc/nixos/develop-gtk.nix?

Thanks!

greetings !

environment.systemPackages

only add executables to path, and doesn’t add thing like header files and libraries that you need for development, buildInputs using mkShell will do that.

nix-shell doesn’t need to be temproray , you can create shell.nix file and commit it to project you are working on

            with import <nixpkgs> ; mkShell {
              nativeBuildInputs = with pkgs; [
                pkg-config
                meson
                ninja
              ];

              buildInputs = with pkgs; [
                gtk4
                libadwaita
                glib
                gsettings-desktop-schemas
                adwaita-icon-theme
              ];
             }
2 Likes

Should be with import <nixpkgs> {};
Also, the with pkgs; is unnecessary since you have the above.

1 Like

yes you are right, it was a very hasty written

Thanks! I would try this setup first. I’m new to NixOS, so sorry if I ask some obvious questions.

I must create a nix-shell file somewhere?

Or I must edit /etc/nixos/develop-gtk.nix?

Or I must do another things?

Nixers typically avoid adding arbitrary software development project concerns to their NixOS configurations. Instead, development shells are used. Those are local to each software project in which they are used. Here’s some documentation:

1 Like

Ok, it’s becoming clearer what to centralize and what to use on the fly. Now I’ll read some documentation. Thanks guys! :+1:

FYI direnv is also quite useful to automatically activate devshells with nix-direnv. Not mandatory by any means, but quite nice. Just add this to your project’s .envrc, next to your shell.nix:

#!/usr/bin/env bash

if ! has nix_direnv_version || ! nix_direnv_version 3.1.2; then
	source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.1.2/direnvrc" "sha256-Di03ad3a0ueGi6CGrfhrQzyGdQIg9APXIPCAMNQgWYM="
fi

use nix
1 Like