commit be8e7ecd69c44627080a8ac54de3812f26264e7e Author: h7x4 Date: Sat Jun 22 16:38:19 2024 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5a3323c --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ + +# Nix +result +result-* + +# CMAKE/vcpkg +build +vcpkg_* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..13db4c5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 3.16) +project(TestProject LANGUAGES CXX) + +find_package(fmt CONFIG REQUIRED) +find_package(nlohmann_json CONFIG REQUIRED) + +# find_package(fmt REQUIRED) +# find_package(nlohmann_json REQUIRED) + +add_executable(test main.cpp) + +target_link_libraries( + test + PRIVATE + fmt::fmt + nlohmann_json::nlohmann_json +) diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..b809cdb --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1718895438, + "narHash": "sha256-k3JqJrkdoYwE3fHE6xGDY676AYmyh4U2Zw+0Bwe5DLU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "d603719ec6e294f034936c0d0dc06f689d91b6c3", + "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..dc1e6d7 --- /dev/null +++ b/flake.nix @@ -0,0 +1,59 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + + outputs = { self, nixpkgs }: let + systems = [ + "x86_64-linux" + "x86_64-darwin" + "aarch64-linux" + "aarch64-darwin" + "armv7l-linux" + ]; + forAllSystems = f: nixpkgs.lib.genAttrs systems (system: let + pkgs = import nixpkgs { + inherit system; + overlays = [ + (prev: final: { + vcpkg-tool = final.callPackage ./nix/packages/vcpkg-tool/package.nix { }; + }) + ]; + }; + + in f system pkgs); + inherit (nixpkgs) lib; + in { + packages = forAllSystems (system: pkgs: { + default = self.packages.${system}.test; + test = pkgs.callPackage ./nix/packages/test/package.nix { + src = ./.; + }; + }); + + devShells = forAllSystems (system: pkgs: { + default = self.devShells.${system}.vcpkgVendoredLibs; + + vcpkgVendoredLibs = pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + vcpkg + cmake + ]; + + env = { + VCPKG_ROOT = "${pkgs.vcpkg}/share/vcpkg"; + }; + }; + + nixVendoredLibs = pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + cmake + ]; + buildInputs = with pkgs; [ + nlohmann_json + fmt + ]; + }; + }); + }; +} diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..4b7027a --- /dev/null +++ b/main.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +int main() +{ + nlohmann::json j; + j["pi"] = 3.141; + j["happy"] = true; + j["name"] = "Niels"; + j["nothing"] = nullptr; + j["answer"]["everything"] = 42; + j["list"] = {1, 0, 2}; + j["object"] = {{"currency", "USD"}, {"value", 42.99}}; + + std::string s = j.dump(); + std::cout << s << std::endl; + + nlohmann::json j2 = nlohmann::json::parse(s); + std::cout << j2.dump(4) << std::endl; + + return 0; +} \ No newline at end of file diff --git a/nix/packages/test/package.nix b/nix/packages/test/package.nix new file mode 100644 index 0000000..522a7c3 --- /dev/null +++ b/nix/packages/test/package.nix @@ -0,0 +1,138 @@ +{ + lib +, src +, stdenv + +, cmake +, vcpkg +, vcpkg-tool +, git +, curl +, cacert +, ninja +, pkg-config +, zip +}: +let + deps = stdenv.mkDerivation { + pname = "test-deps"; + version = "0.0.1"; + inherit src; + + outputHashAlgo = "sha256"; + outputHashMode = "recursive"; + outputHash = { + "x86_64-linux" = "sha256-nFIbhVp32y+TlFLhuLc0XhlGJZmADgKBo4pd78H3mg8="; + # "x86_64-linux" = ""; + "aarch64-linux" = ""; + "armv7l-linux" = null; + }.${stdenv.hostPlatform.system}; + + dontConfigure = true; + + env = { + VCPKG_ROOT = "${vcpkg}/share/vcpkg"; + }; + + nativeBuildInputs = [ + # TODO: is this a direct $PATH dependency of `vcpkg install`? + cmake + + # TODO: fix overriding the envvars in build script + # vcpkg + + # TODO: wrap these in the $PATH of vcpkg-tool + vcpkg-tool + git + curl + cacert + ninja + zip + + # Only for fmt? + pkg-config + ]; + + buildPhase = '' + runHook preBuild + + mkdir -p "$out" + + vcpkg install \ + --x-buildtrees-root="$TMP/vcpkg_buildtrees" \ + --x-packages-root="$TMP/vcpkg_packages" \ + --only-downloads \ + --downloads-root="$out/vcpkg_downloads" + + runHook postBuild + ''; + # --triplet="$VCPKG_DEFAULT_TRIPLET" \ + # --host-triplet="$VCPKG_DEFAULT_HOST_TRIPLET" + + dontInstall = true; + }; +in +stdenv.mkDerivation { + pname = "test"; + version = "0.0.1"; + inherit src; + + nativeBuildInputs = [ + cmake + + vcpkg-tool + git + curl + cacert + ninja + zip + ]; + + dontConfigure = true; + + env = { + VCPKG_ROOT = "${vcpkg}/share/vcpkg"; + }; + + buildPhase = let + vcpkg-install-opts = lib.concatStringsSep " " [ + ''--x-buildtrees-root=$TMP/vcpkg_buildtrees'' + ''--x-packages-root=$TMP/vcpkg_packages'' + ''--downloads-root=${deps}'' + ''--x-manifest-root=.'' + ''--x-install-root=$TMP/vcpkg_installed'' + ]; + + cmake-opts = lib.concatStringsSep " " [ + (lib.cmakeFeature "VCPKG_INSTALLED_DIR" "$TMP/vcpkg_installed") + (lib.cmakeFeature "VCPKG_TRACE_FIND_PACKAGE" "ON") + (lib.cmakeFeature "VCPKG_MANIFEST_INSTALL" "OFF") + (lib.cmakeFeature "CMAKE_BUILD_TYPE" "Release") + (lib.cmakeFeature "CMAKE_TOOLCHAIN_FILE" "${vcpkg}/share/vcpkg/scripts/buildsystems/vcpkg.cmake") + # (lib.cmakeFeature "CMAKE_CXX_COMPILER" "g++") + # (lib.cmakeFeature "CMAKE_C_COMPILER" "gcc") + # -DVCPKG_INSTALL_OPTIONS="${vcpkg-install-opts}" + ]; + in '' + runHook preBuild + + vcpkg install ${vcpkg-install-opts} + + cmake ${cmake-opts} -S . -B build -G Ninja + cmake --build ./build --config Release + + runHook postBuild + ''; + + # dontInstall = true; + installPhase = '' + runHook preInstall + + mkdir "$out" + cmake --install ./build --prefix "$out" + + runHook postInstall + ''; + + passthru.deps = deps; +} diff --git a/nix/packages/vcpkg-tool/change-lock-location.patch b/nix/packages/vcpkg-tool/change-lock-location.patch new file mode 100644 index 0000000..d3cf8e0 --- /dev/null +++ b/nix/packages/vcpkg-tool/change-lock-location.patch @@ -0,0 +1,14 @@ +diff --git a/src/vcpkg/vcpkgpaths.cpp b/src/vcpkg/vcpkgpaths.cpp +index 3f588c21..e6f2bbed 100644 +--- a/src/vcpkg/vcpkgpaths.cpp ++++ b/src/vcpkg/vcpkgpaths.cpp +@@ -579,7 +579,8 @@ namespace vcpkg + if (!args.do_not_take_lock) + { + std::error_code ec; +- const auto vcpkg_root_file = root / ".vcpkg-root"; ++ fs.create_directories(Path{"/tmp/vcpkg"}, VCPKG_LINE_INFO); ++ const auto vcpkg_root_file = Path{"/tmp/vcpkg"} / Hash::get_string_sha256(root.c_str()); + if (args.wait_for_lock.value_or(false)) + { + file_lock_handle = fs.take_exclusive_file_lock(vcpkg_root_file, ec); diff --git a/nix/packages/vcpkg-tool/package.nix b/nix/packages/vcpkg-tool/package.nix new file mode 100644 index 0000000..8d47943 --- /dev/null +++ b/nix/packages/vcpkg-tool/package.nix @@ -0,0 +1,77 @@ +{ lib +, stdenv +, fetchFromGitHub +, cacert +, cmake +, cmakerc +, fmt +, git +, gzip +, makeWrapper +, meson +, ninja +, openssh +, python3 +, zip +, zstd +, extraRuntimeDeps ? [] +}: +stdenv.mkDerivation (finalAttrs: { + pname = "vcpkg-tool"; + version = "2024-06-10"; + + src = fetchFromGitHub { + owner = "microsoft"; + repo = "vcpkg-tool"; + rev = finalAttrs.version; + hash = "sha256-TGRTzUd1FtErD+h/ksUsUm1Rhank9/yVy06JbAgEEw0="; + }; + + nativeBuildInputs = [ + cmake + ninja + makeWrapper + ]; + + buildInputs = [ + fmt + cmakerc + ]; + + patches = [ + ./change-lock-location.patch + ]; + + cmakeFlags = [ + "-DVCPKG_DEPENDENCY_EXTERNAL_FMT=ON" + "-DVCPKG_DEPENDENCY_CMAKERC=ON" + ]; + + postFixup = let + # These are the most common binaries used by vcpkg + # Extra binaries can be added via overlay when needed + runtimeDeps = [ + cacert + cmake + git + gzip + meson + ninja + openssh + python3 + zip + zstd + ] ++ extraRuntimeDeps; + in '' + wrapProgram $out/bin/vcpkg --prefix PATH ${lib.makeBinPath runtimeDeps} + ''; + + meta = { + description = "Components of microsoft/vcpkg's binary"; + mainProgram = "vcpkg"; + homepage = "https://github.com/microsoft/vcpkg-tool"; + license = lib.licenses.mit; + maintainers = with lib.maintainers; [ guekka gracicot ]; + platforms = lib.platforms.all; + }; +}) diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json new file mode 100644 index 0000000..5274e2d --- /dev/null +++ b/vcpkg-configuration.json @@ -0,0 +1,9 @@ +{ + "registries": [ + { + "kind": "artifact", + "location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip", + "name": "microsoft" + } + ] +} diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 0000000..b49fc5b --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,10 @@ +{ + "name": "nix-vcpkg-cross-experiment", + "version": "0.0.1", + "description": "Example of (cross) compiling vcpkg-dependant software with nix", + "license": "MIT", + "dependencies": [ + "fmt", + "nlohmann-json" + ] +}