templates/basic-nix-cpp-app: init

This commit is contained in:
Gabriel Arazas 2024-02-17 14:26:33 +08:00
parent 03590ad834
commit 85c29f0e63
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
6 changed files with 113 additions and 0 deletions

View File

@ -6,6 +6,10 @@
path = ../../templates/basic-devshell;
description = "Basic development shell template";
};
basic-nix-cpp-app = {
path = ../../templates/basic-nix-cpp-app;
description = "Basic Nix program with C++ API";
};
basic-nix-module-flake = {
path = ../../templates/basic-nix-module-flake;
description = "Basic Nix module flake template";

View File

@ -0,0 +1,37 @@
{ stdenv
, lib
, cmake
, meson
, ninja
, pkg-config
, boost
, nix
, semver-cpp
}:
stdenv.mkDerivation {
pname = "app";
version = "0.1.0";
src = ./.;
nativeBuildInputs = [
meson
ninja
pkg-config
];
buildInputs = [
cmake
boost
nix
semver-cpp
];
meta = with lib; {
description = "Basic Nix CLI";
license = licenses.mit;
maintainers = with maintainers; [ foo-dogsquared ];
platforms = nix.meta.platforms;
};
}

View File

@ -0,0 +1,11 @@
project('app',
'cpp',
version: '0.1.0',
license: 'MIT',
meson_version: '>=0.56.0',
default_options: [
'cpp_std=c++20'
]
)
subdir('src')

View File

@ -0,0 +1,13 @@
{ pkgs ? import <nixpkgs> { } }:
let
app = pkgs.callPackage ./. { };
in
pkgs.mkShell {
inputsFrom = [ app ];
packages = with pkgs; [
git
clang-tools
];
}

View File

@ -0,0 +1,29 @@
// A nifty little app for resolving flakerefs to the locked input. Because the
// C++ API is mostly unstable, this is confirmed to be working with Nix v2.18.2
// just for reference.
#include <iostream>
#include <nlohmann/json.hpp>
#include <nix/flake/flake.hh>
#include <nix/shared.hh>
#include <nix/store-api.hh>
int main(int argc, char *argv[]) {
nix::initNix();
auto store = nix::openStore();
try {
nix::FlakeRef originalRef =
nix::parseFlakeRef(argv[1], std::nullopt, true, false);
auto [tree, lockedInput] = originalRef.input.fetch(store);
std::cout << lockedInput.to_string() << std::endl;
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return EXIT_SUCCESS;
}
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,19 @@
app_srcs = [
'main.cc',
]
NIX_VERSION = '>=2.17'
deps = [
dependency('nix-cmd', version: NIX_VERSION),
dependency('nix-main', version: NIX_VERSION),
dependency('nix-expr', version: NIX_VERSION),
dependency('nix-store', version: NIX_VERSION),
]
executable('app',
app_srcs,
dependencies: deps,
install: true,
link_args: '-Wl,-lnixfetchers'
)