flake.nix 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. {
  2. description = "QMD - Quick Markdown Search";
  3. inputs = {
  4. nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  5. flake-utils.url = "github:numtide/flake-utils";
  6. };
  7. outputs = { self, nixpkgs, flake-utils }:
  8. flake-utils.lib.eachDefaultSystem (system:
  9. let
  10. pkgs = nixpkgs.legacyPackages.${system};
  11. # SQLite with loadable extension support for sqlite-vec
  12. sqliteWithExtensions = pkgs.sqlite.overrideAttrs (old: {
  13. configureFlags = (old.configureFlags or []) ++ [
  14. "--enable-load-extension"
  15. ];
  16. });
  17. nodeModulesHashes = {
  18. x86_64-linux = "sha256-Hymzuiid76j0LbDRACYlRQ2UVxQp7t9xg4nH37l0Keg=";
  19. aarch64-darwin = "sha256-/9kp5mNrI7hVR137DRpSuZHnl1RL/wFu2hKyzXW66TU=";
  20. # Populate these on first build for additional hosts if/when needed.
  21. aarch64-linux = pkgs.lib.fakeHash;
  22. x86_64-darwin = pkgs.lib.fakeHash;
  23. };
  24. nodeModules = pkgs.stdenvNoCC.mkDerivation {
  25. pname = "qmd-node-modules";
  26. version = "1.0.0";
  27. src = ./.;
  28. impureEnvVars = pkgs.lib.fetchers.proxyImpureEnvVars ++ [
  29. "GIT_PROXY_COMMAND"
  30. "SOCKS_SERVER"
  31. ];
  32. nativeBuildInputs = [
  33. pkgs.bun
  34. ];
  35. dontConfigure = true;
  36. buildPhase = ''
  37. export HOME=$(mktemp -d)
  38. bun install \
  39. --backend copyfile \
  40. --frozen-lockfile \
  41. --ignore-scripts \
  42. --no-progress \
  43. --production
  44. '';
  45. installPhase = ''
  46. mkdir -p $out
  47. cp -R node_modules $out/
  48. '';
  49. dontFixup = true;
  50. outputHash = nodeModulesHashes.${system};
  51. outputHashAlgo = "sha256";
  52. outputHashMode = "recursive";
  53. };
  54. qmd = pkgs.stdenv.mkDerivation {
  55. pname = "qmd";
  56. version = "1.0.0";
  57. src = ./.;
  58. nativeBuildInputs = [
  59. pkgs.bun
  60. pkgs.makeWrapper
  61. pkgs.nodejs
  62. pkgs.node-gyp
  63. pkgs.python3 # needed by node-gyp to compile better-sqlite3
  64. ] ++ pkgs.lib.optionals pkgs.stdenv.hostPlatform.isDarwin [
  65. pkgs.darwin.cctools # provides libtool needed by node-gyp on macOS
  66. ];
  67. buildInputs = [ pkgs.sqlite ];
  68. buildPhase = ''
  69. export HOME=$(mktemp -d)
  70. cp -R ${nodeModules}/node_modules ./
  71. chmod -R u+w node_modules
  72. (cd node_modules/better-sqlite3 && node-gyp rebuild --release)
  73. '';
  74. installPhase = ''
  75. mkdir -p $out/lib/qmd
  76. mkdir -p $out/bin
  77. cp -r node_modules $out/lib/qmd/
  78. cp -r src $out/lib/qmd/
  79. cp package.json $out/lib/qmd/
  80. makeWrapper ${pkgs.bun}/bin/bun $out/bin/qmd \
  81. --add-flags "$out/lib/qmd/src/cli/qmd.ts" \
  82. --set DYLD_LIBRARY_PATH "${pkgs.sqlite.out}/lib" \
  83. --set LD_LIBRARY_PATH "${pkgs.sqlite.out}/lib"
  84. '';
  85. meta = with pkgs.lib; {
  86. description = "On-device search engine for markdown notes, meeting transcripts, and knowledge bases";
  87. homepage = "https://github.com/tobi/qmd";
  88. license = licenses.mit;
  89. platforms = platforms.unix;
  90. };
  91. };
  92. in
  93. {
  94. packages = {
  95. default = qmd;
  96. qmd = qmd;
  97. };
  98. apps.default = {
  99. type = "app";
  100. program = "${qmd}/bin/qmd";
  101. };
  102. devShells.default = pkgs.mkShell {
  103. buildInputs = [
  104. pkgs.bun
  105. sqliteWithExtensions
  106. ];
  107. shellHook = ''
  108. export BREW_PREFIX="''${BREW_PREFIX:-${sqliteWithExtensions.out}}"
  109. echo "QMD development shell"
  110. echo "Run: bun src/cli/qmd.ts <command>"
  111. '';
  112. };
  113. }
  114. );
  115. }