From e465d052849148ac458b327fbbee4b4f90ed0d89 Mon Sep 17 00:00:00 2001 From: h4rl Date: Mon, 9 Jun 2025 21:56:18 +0200 Subject: [PATCH 1/4] feat: add nix flake --- .gitignore | 5 +- flake.lock | 27 +++++++++ flake.nix | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.gitignore b/.gitignore index 920b172..40c3ccb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ cmake-build-release/ .idea/ node_modules/ *.dSYM -.vs/ \ No newline at end of file +.vs/ +.direnv/ +.envrc +build/ diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..b450a06 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1747327360, + "narHash": "sha256-LSmTbiq/nqZR9B2t4MRnWG7cb0KVNU70dB7RT4+wYK4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "e06158e58f3adee28b139e9c2bcfcc41f8625b46", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..ff2d70f --- /dev/null +++ b/flake.nix @@ -0,0 +1,164 @@ +{ + description = "Clay (short for C Layout) is a high performance 2D UI layout library."; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + + outputs = { + self, + nixpkgs, + }: let + inherit (nixpkgs) lib; + systems = lib.systems.flakeExposed; + forAllSystems = lib.genAttrs systems; + nixpkgsFor = forAllSystems (system: nixpkgs.legacyPackages.${system}); + in { + checks = forAllSystems ( + system: let + pkgs = nixpkgsFor.${system}; + in { + formatting = + pkgs.runCommand "format-flake" { + nativeBuildInputs = with pkgs; [ + alejandra + deadnix + statix + ]; + } '' + echo "Checking nix formatting" + alejandra --check . + + echo "Checking dead code" + deadnix --fail . + + echo "Checking statix" + statix check . + ''; + } + ); + + devShells = forAllSystems ( + system: let + pkgs = nixpkgsFor.${system}; + in { + default = pkgs.mkShell { + # For testing renderers and examples, aswell as tests + packages = with pkgs; [ + # Renderers + SDL2 + SDL2_image + SDL2_ttf + + sdl3 + sdl3-ttf + sdl3-image + + cairo + raylib + sokol + + cmake # this is needed for building examples and tests + gcc + clang-tools # for clangd and clang-tidy + pkg-config + alejandra # for formatting nix + + # for tests + docker + docker-compose + + # dependencies for examples and tests + xorg.libXrandr + xorg.libXinerama + xorg.libXcursor + xorg.libXi + ]; + + LD_LIBRARY_PATH = "${lib.makeLibraryPath [ + # Renderers + pkgs.SDL2 + pkgs.SDL2_image + pkgs.SDL2_ttf + + pkgs.sdl3 + pkgs.sdl3-ttf + pkgs.sdl3-image + + pkgs.cairo + pkgs.raylib + pkgs.sokol + + # dependencies for examples and tests + pkgs.xorg.libXrandr + pkgs.xorg.libXinerama + pkgs.xorg.libXcursor + pkgs.xorg.libXi + ]}"; + }; + } + ); + + formatter = forAllSystems (system: nixpkgsFor.${system}.alejandra); + + packages = forAllSystems ( + system: let + pkgs = nixpkgsFor.${system}; + git_tag + date = let + # YYYYMMDD + date' = lib.substring 0 8 self.lastModifiedDate; + year = lib.substring 0 4 date'; + month = lib.substring 4 2 date'; + date = lib.substring 6 2 date'; + in + if (self ? "lastModifiedDate") + then + lib.concatStringsSep "-" [ + year + month + date + ] + else "unknown"; + in { + default = pkgs.stdenv.mkDerivation (finalAttrs { + pname = "clay"; + version = "v0.14-master-${date}"; + src = ./.; + + dontBuild = true; + + installPhase = '' + mkdir -p $out/include + mkdir -p $out/lib/pkgconfig + + cp clay.h $out/include + + cat <$out/lib/pkgconfig/clay.pc + prefix=$out + includedir=$out/include + Name: Clay + Description: Clay (short for C Layout) is a high performance 2D UI layout library. + Version: $version + Cflags: -I$out/include + EOF + ''; + + passthru.tests.pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage; + + meta = with lib; { + description = "Clay (short for C Layout) is a high performance 2D UI layout library."; + homepage = "https://github.com/nicbarker/clay"; + license = licenses.zlib; + maintainers = with maintainers; [nicbarker]; + pkgConfigModules = ["clay"]; + # FIXME: I'm not sure what platforms this supports. + platforms = platforms.all; + }; + }); + + defaultPackage = self.packages.${system}.default; + } + ); + }; +} From e1a4fb9810483f4966748b6e302b7bf25d160812 Mon Sep 17 00:00:00 2001 From: h4rl Date: Mon, 9 Jun 2025 22:52:50 +0200 Subject: [PATCH 2/4] refactor: fix flake checks, make package definitions easier --- .gitignore | 1 + flake.lock | 6 +-- flake.nix | 138 +++++++++++++++++++++++++++++++---------------------- 3 files changed, 85 insertions(+), 60 deletions(-) diff --git a/.gitignore b/.gitignore index 40c3ccb..41e3248 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ node_modules/ .direnv/ .envrc build/ +result/ diff --git a/flake.lock b/flake.lock index b450a06..cd2b62f 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1747327360, - "narHash": "sha256-LSmTbiq/nqZR9B2t4MRnWG7cb0KVNU70dB7RT4+wYK4=", + "lastModified": 1749285348, + "narHash": "sha256-frdhQvPbmDYaScPFiCnfdh3B/Vh81Uuoo0w5TkWmmjU=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "e06158e58f3adee28b139e9c2bcfcc41f8625b46", + "rev": "3e3afe5174c561dee0df6f2c2b2236990146329f", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index ff2d70f..0b949bd 100644 --- a/flake.nix +++ b/flake.nix @@ -10,9 +10,62 @@ nixpkgs, }: let inherit (nixpkgs) lib; + systems = lib.systems.flakeExposed; forAllSystems = lib.genAttrs systems; nixpkgsFor = forAllSystems (system: nixpkgs.legacyPackages.${system}); + + date = let + # YYYYMMDD + date' = lib.substring 0 8 self.lastModifiedDate; + year = lib.substring 0 4 date'; + month = lib.substring 4 2 date'; + day = lib.substring 6 2 date'; + in + if (self ? lastModifiedDate) + then lib.concatStringsSep "-" [year month day] + else "unknown"; + + mkClayPackage = { + system, + version, + src, + suffix, + }: + nixpkgsFor.${system}.stdenv.mkDerivation { + pname = "clay"; + version = "${version}${suffix}"; + inherit src; + + dontBuild = true; + + postInstall = '' + mkdir -p $out/include + mkdir -p $out/lib/pkgconfig + + cp clay.h $out/include + + cat <$out/lib/pkgconfig/clay.pc + prefix=$out + includedir=$out/include + Name: Clay + Description: Clay (short for C Layout) is a high performance 2D UI layout library. + Version: ${version} + Cflags: -I$out/include + EOF + ''; + + passthru.tests.pkg-config = nixpkgs.lib.testing.testMetaPkgConfig self.packages.${system}.${version}; + + meta = with lib; { + description = "Clay (short for C Layout) is a high performance 2D UI layout library."; + homepage = "https://github.com/nicbarker/clay"; + license = licenses.zlib; + maintainers = with maintainers; [nicbarker]; + pkgConfigModules = ["clay"]; + platforms = platforms.all; + }; + }; in { checks = forAllSystems ( system: let @@ -26,6 +79,8 @@ statix ]; } '' + cd ${self} + echo "Checking nix formatting" alejandra --check . @@ -34,6 +89,8 @@ echo "Checking statix" statix check . + + touch $out ''; } ); @@ -101,64 +158,31 @@ formatter = forAllSystems (system: nixpkgsFor.${system}.alejandra); - packages = forAllSystems ( - system: let - pkgs = nixpkgsFor.${system}; - git_tag - date = let - # YYYYMMDD - date' = lib.substring 0 8 self.lastModifiedDate; - year = lib.substring 0 4 date'; - month = lib.substring 4 2 date'; - date = lib.substring 6 2 date'; - in - if (self ? "lastModifiedDate") - then - lib.concatStringsSep "-" [ - year - month - date - ] - else "unknown"; - in { - default = pkgs.stdenv.mkDerivation (finalAttrs { - pname = "clay"; - version = "v0.14-master-${date}"; - src = ./.; + packages = forAllSystems (system: let + pkgs = nixpkgsFor.${system}; + inherit (pkgs) fetchFromGitHub; + in { + stable = mkClayPackage { + inherit system; + version = "0.14"; + # To update this use nurl https://github.com/nicbarker/clay {tag} + src = fetchFromGitHub { + owner = "nicbarker"; + repo = "clay"; + rev = "v0.14"; # This will need to be updated when a new version is released + hash = "sha256-6h1aQXqwzPc4oPuid3RfV7W0WzQFUiddjW7OtkKM0P8="; # This too likely + }; + suffix = ""; + }; - dontBuild = true; + unstable = mkClayPackage { + inherit system; + version = "0.14"; + src = ./.; + suffix = "-master-${date}"; + }; - installPhase = '' - mkdir -p $out/include - mkdir -p $out/lib/pkgconfig - - cp clay.h $out/include - - cat <$out/lib/pkgconfig/clay.pc - prefix=$out - includedir=$out/include - Name: Clay - Description: Clay (short for C Layout) is a high performance 2D UI layout library. - Version: $version - Cflags: -I$out/include - EOF - ''; - - passthru.tests.pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage; - - meta = with lib; { - description = "Clay (short for C Layout) is a high performance 2D UI layout library."; - homepage = "https://github.com/nicbarker/clay"; - license = licenses.zlib; - maintainers = with maintainers; [nicbarker]; - pkgConfigModules = ["clay"]; - # FIXME: I'm not sure what platforms this supports. - platforms = platforms.all; - }; - }); - - defaultPackage = self.packages.${system}.default; - } - ); + default = self.packages.${system}.stable; + }); }; } From 7af4d9d3f0d0d3f7405bd728947c29b405d8e6c0 Mon Sep 17 00:00:00 2001 From: h4rl Date: Tue, 10 Jun 2025 02:08:54 +0200 Subject: [PATCH 3/4] feat(flake): move to build inputs and remove unnecessary packages --- flake.nix | 39 +++++++-------------------------------- 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/flake.nix b/flake.nix index 0b949bd..f2d9182 100644 --- a/flake.nix +++ b/flake.nix @@ -102,7 +102,13 @@ default = pkgs.mkShell { # For testing renderers and examples, aswell as tests packages = with pkgs; [ - # Renderers + cmake # this is needed for building examples and tests + clang-tools # for clangd and clang-tidy + pkg-config + alejandra # for formatting nix + ]; + + buildInputs = with pkgs; [ SDL2 SDL2_image SDL2_ttf @@ -115,43 +121,12 @@ raylib sokol - cmake # this is needed for building examples and tests - gcc - clang-tools # for clangd and clang-tidy - pkg-config - alejandra # for formatting nix - - # for tests - docker - docker-compose - # dependencies for examples and tests xorg.libXrandr xorg.libXinerama xorg.libXcursor xorg.libXi ]; - - LD_LIBRARY_PATH = "${lib.makeLibraryPath [ - # Renderers - pkgs.SDL2 - pkgs.SDL2_image - pkgs.SDL2_ttf - - pkgs.sdl3 - pkgs.sdl3-ttf - pkgs.sdl3-image - - pkgs.cairo - pkgs.raylib - pkgs.sokol - - # dependencies for examples and tests - pkgs.xorg.libXrandr - pkgs.xorg.libXinerama - pkgs.xorg.libXcursor - pkgs.xorg.libXi - ]}"; }; } ); From 500fab288e37b621b69f8b60df85be03ce762219 Mon Sep 17 00:00:00 2001 From: h4rl Date: Tue, 10 Jun 2025 18:45:24 +0200 Subject: [PATCH 4/4] feat(flake): expose unstable by default, i need some help figuring out how to export both packages --- .gitignore | 4 ++-- flake.nix | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 41e3248..d46fb21 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,5 @@ node_modules/ .vs/ .direnv/ .envrc -build/ -result/ +/build/ +/result/ diff --git a/flake.nix b/flake.nix index f2d9182..6e4c9e3 100644 --- a/flake.nix +++ b/flake.nix @@ -103,9 +103,11 @@ # For testing renderers and examples, aswell as tests packages = with pkgs; [ cmake # this is needed for building examples and tests - clang-tools # for clangd and clang-tidy + clang-tools # for clangd and clang-tidy (optional) pkg-config alejandra # for formatting nix + + lld # for wasm ]; buildInputs = with pkgs; [ @@ -157,7 +159,9 @@ suffix = "-master-${date}"; }; - default = self.packages.${system}.stable; + # I have no idea how to expose both unstable and stable at the same time + # so I'm going to just expose the unstable version for now + default = self.packages.${system}.unstable; }); }; }