Gl and opengl for C++, CMake dev?

Hi.
Trying to use C++ + CMake shows that opengl, gl and vulkan, cant find them.

services.xserver.videoDrivers = [ "amdgpu" ];
hardware.opengl.enable = true;
hardware.opengl.driSupport = true;
hardware.opengl.driSupport32Bit = true;
environment.systemPackages = with pkgs; [
  libGL libGLU
];
environment.variables = {
  LD_LIBRARY_PATH = "$LD_LIBRARY_PATH:/run/opengl-driver/lib:/run/opengl-driver-32/lib";
};

nixos doesn’t install headers and cmake files by default
if you want to compile with libraries etc
you can use nix-shell -p {your libraries} for quickly experimenting
or creating nix development environment using pkgs.mkShell and adding
libraries to buildInputs

and you probably dont need to add opengl drivers to LD_LIBRARY_PATH

1 Like

Thank you.
Yeah, reading about that, in reddit recommend me to use Development environment.
As a new NixOS user, still don’t understand about flask, home manager and Dev Env. And I just add everything to the configuration.nix file.

Could you share some example about this ?

This is my config:
https://justpaste.it/NixOSConfig

{ pkgs ? import <nixpkgs> { } }:
pkgs.llvmPackages.stdenv.mkDerivation {
  name = "some-name";
  nativeBuildInputs = with pkgs;[ cmake pkg-config ];
  buildInputs = with pkgs;[
    glfw
    glm
    lua
  ];
} 

here is a simple definition for shell.nix you will need to add runtime packages(and libraries) to buildInputs and build tools(like shader compilers build systems) to nativeBuildInputs and if you want to change compiler you can use diffrent stdenv like pkgs.stdenv or pkgs.llvmPackages.stdenv(this is for clang).

After writing this to shell.nix in root dir of your project run nix-shell and your development shell is ready

Note: when creating a package you should use pname and version instead of directly setting name attribute but this shouldnt matter for creating devenv

mkShell is just some modified version of mkDerivation, i used mkDerivation because i can select compiler

Also check out Development environment with nix-shell - NixOS Wiki it explains things with more detail

1 Like

Thank you so much, I will check it :smiley:

Sorry, I started learning flask and home-manager, and trying to implemente the flake.
Still getting the gl problem:
Note: This code is from some else.


{
  description = "C/C++ environment";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, utils, ... }@inputs:
    utils.lib.eachDefaultSystem (
      system:
      let
        p = import nixpkgs { 
				inherit system;
				config = { 
					allowUnfree = true; 
				}; 
			};
        llvm = p.llvmPackages_latest;

        # simple script which replaces the functionality of make
        # it works with <math.h> and includes debugging symbols by default
        # it will be updated as per needs

        # arguments: outfile
        # basic usage example: mk main [flags]
        mymake = p.writeShellScriptBin "mk" ''
          if [ -f "$1.c" ]; then
            i="$1.c"
            c=$CC
          else
            i="$1.cpp"
            c=$CXX
          fi
          o=$1
          shift
          $c -ggdb $i -o $o -lm -Wall $@
        '';
      in
      {
        devShell = p.mkShell.override { stdenv = p.clangStdenv; } rec {
          packages = with p; [
            # builder
            gnumake
            cmake
			ninja
            bear

            # debugger
            llvm.lldb
            gdb

            # fix headers not found
            clang-tools

            # LSP and compiler
            llvm.libstdcxxClang

            # other tools
            cppcheck
            llvm.libllvm
            valgrind
            mymake
			conan

            # stdlib for cpp
            llvm.libcxx
              
            # libs
			libGL
			libGLU
			faac
			faad2
			freeglut
			glew
			glfw
            glm
            SDL2
			SDL2_ttf
			SDL2_net
			SDL2_gfx
			SDL2_sound
			SDL2_mixer
			SDL2_image
          ];
          name = "C";
        };
      }
    );
}

Then nix develop

Can you paste exact error message ?
And wheater error is from cmake ,when compiling or when running compiled program ?

Thank you I made it :smiley:

But I have some questions about the “normal” use of NixOS vs Flakes, and in this project of develop.