aboutsummaryrefslogtreecommitdiffstats
path: root/flake.nix
blob: 2850520d8fd6b0b384c6519718c2b7ae41c92e49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
{
  description = "RelationalNT CMake build";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    picosha2 = {
      url = "github:okdshin/PicoSHA2";
      flake = false;
    };
  };

  outputs = { self, nixpkgs, picosha2 }:
    let
      systems = [
        "x86_64-linux"
        "aarch64-linux"
        "x86_64-darwin"
        "aarch64-darwin"
      ];

      forAllSystems = nixpkgs.lib.genAttrs systems;

      # Shared per-system derivations used by both the package and the dev shell.
      mkDeps = system:
        let
          pkgs = import nixpkgs { inherit system; };

          # Maps the vcpkg-style unofficial::sqlite3::sqlite3 CMake
          # target to the system SQLite3 package. Required because CMakeLists.txt
          # calls target_link_libraries(... unofficial::sqlite3::sqlite3).
          unofficialSqlite3Config = pkgs.writeTextDir
            "share/unofficial-sqlite3/unofficial-sqlite3-config.cmake"
            ''
              include(CMakeFindDependencyMacro)
              find_dependency(SQLite3 REQUIRED)

              if(NOT TARGET unofficial::sqlite3::sqlite3)
                add_library(unofficial::sqlite3::sqlite3 INTERFACE IMPORTED)
                target_link_libraries(unofficial::sqlite3::sqlite3 INTERFACE SQLite::SQLite3)
              endif()
            '';

          # PicoSHA2 is a single-header library; extract just the header.
          picosha2Headers = pkgs.runCommand "picosha2-headers" { } ''
            install -Dm644 ${picosha2}/picosha2.h $out/include/picosha2.h
          '';

        in { inherit pkgs unofficialSqlite3Config picosha2Headers; };

    in
    {
      packages = forAllSystems (system:
        let
          inherit (mkDeps system) pkgs unofficialSqlite3Config picosha2Headers;
        in
        {
          default = pkgs.stdenv.mkDerivation {
            pname = "relationalnt";
            version = "0.1.0";

            src = self;

            nativeBuildInputs = [
              pkgs.cmake
              pkgs.ninja
            ];

            buildInputs = [
              pkgs.catch2_3
              picosha2Headers
              pkgs.sqlite
              unofficialSqlite3Config
            ];

            cmakeFlags = [
              "-DCMAKE_BUILD_TYPE=Release"
            ];

            doCheck = true;
            checkPhase = ''
              runHook preCheck
              ctest --output-on-failure
              runHook postCheck
            '';

            installPhase = ''
              runHook preInstall
              install -Dm755 RelationalNT "$out/bin/RelationalNT"
              install -Dm755 RNT_tests "$out/bin/RNT_tests"
              for lib in libRNT.so libRNT.dylib; do
                if [ -f "$lib" ]; then
                  install -Dm755 "$lib" "$out/lib/$lib"
                fi
              done
              install -Dm644 ../include/RNT_C_API.h "$out/include/RNT_C_API.h"
              runHook postInstall
            '';

            meta = {
              description = "Experimental database kernel inspired by the Windows NT object manager";
              mainProgram = "RelationalNT";
            };
          };
        });

      devShells = forAllSystems (system:
        let
          inherit (mkDeps system) pkgs unofficialSqlite3Config picosha2Headers;

          # Wrapper that invokes clangd with the Nix toolchain's compiler paths,
          # enabling accurate diagnostics in editors that use compile_commands.json.
          clangdRnt = pkgs.writeShellScriptBin "clangd-rnt" ''
            exec ${pkgs.clang-tools}/bin/clangd \
              --query-driver='${pkgs.stdenv.cc}/bin/*,/usr/bin/clang,/usr/bin/clang++' \
              "$@"
          '';
        in
        {
          default = pkgs.mkShell {
            packages = [
              pkgs.catch2_3
              pkgs.clang-tools
              pkgs.cmake
              pkgs.lldb
              pkgs.ninja
              clangdRnt
              picosha2Headers
              pkgs.sqlite
              unofficialSqlite3Config
            ];

            CC = "${pkgs.stdenv.cc}/bin/cc";
            CXX = "${pkgs.stdenv.cc}/bin/c++";

            CMAKE_PREFIX_PATH = pkgs.lib.makeSearchPathOutput "dev" "" [
              picosha2Headers
              pkgs.sqlite
              unofficialSqlite3Config
            ];

            CMAKE_INCLUDE_PATH = pkgs.lib.makeSearchPathOutput "dev" "include" [
              picosha2Headers
              pkgs.sqlite
            ];

            CMAKE_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
              pkgs.sqlite
            ];

            CPATH = pkgs.lib.makeSearchPathOutput "dev" "include" [
              picosha2Headers
              pkgs.sqlite
            ];

            C_INCLUDE_PATH = pkgs.lib.makeSearchPathOutput "dev" "include" [
              pkgs.sqlite
            ];

            CPLUS_INCLUDE_PATH = pkgs.lib.makeSearchPathOutput "dev" "include" [
              picosha2Headers
              pkgs.sqlite
            ];

            LIBRARY_PATH = pkgs.lib.makeLibraryPath [
              pkgs.sqlite
            ];

            DYLD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
              pkgs.sqlite
            ];

            PKG_CONFIG_PATH = pkgs.lib.makeSearchPathOutput "dev" "lib/pkgconfig" [
              pkgs.sqlite
            ];

            shellHook = ''
              export NIX_CFLAGS_COMPILE="-I${picosha2Headers}/include -I${pkgs.sqlite.dev}/include $NIX_CFLAGS_COMPILE"
              export NIX_LDFLAGS="-L${pkgs.sqlite.out}/lib $NIX_LDFLAGS"
            '';
          };
        });

      apps = forAllSystems (system: {
        default = {
          type = "app";
          program = "${self.packages.${system}.default}/bin/RelationalNT";
        };
      });
    };
}