flake.nix 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. {
  2. description = "CDK Flake";
  3. inputs = {
  4. nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
  5. rust-overlay = {
  6. url = "github:oxalica/rust-overlay";
  7. inputs = {
  8. nixpkgs.follows = "nixpkgs";
  9. };
  10. };
  11. fenix = {
  12. url = "github:nix-community/fenix";
  13. inputs.nixpkgs.follows = "nixpkgs";
  14. inputs.rust-analyzer-src.follows = "";
  15. };
  16. flake-utils.url = "github:numtide/flake-utils";
  17. crane = {
  18. url = "github:ipetkov/crane";
  19. };
  20. pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
  21. };
  22. outputs =
  23. { self
  24. , nixpkgs
  25. , rust-overlay
  26. , flake-utils
  27. , pre-commit-hooks
  28. , ...
  29. }@inputs:
  30. flake-utils.lib.eachDefaultSystem (
  31. system:
  32. let
  33. overlays = [ (import rust-overlay) ];
  34. lib = pkgs.lib;
  35. stdenv = pkgs.stdenv;
  36. isDarwin = stdenv.isDarwin;
  37. libsDarwin =
  38. with pkgs;
  39. lib.optionals isDarwin [
  40. # Additional darwin specific inputs can be set here
  41. darwin.apple_sdk.frameworks.Security
  42. darwin.apple_sdk.frameworks.SystemConfiguration
  43. ];
  44. # Dependencies
  45. pkgs = import nixpkgs {
  46. inherit system overlays;
  47. };
  48. # Toolchains
  49. # latest stable
  50. stable_toolchain = pkgs.rust-bin.stable."1.91.1".default.override {
  51. targets = [ "wasm32-unknown-unknown" ]; # wasm
  52. extensions = [
  53. "rustfmt"
  54. "clippy"
  55. "rust-analyzer"
  56. ];
  57. };
  58. # MSRV stable
  59. msrv_toolchain = pkgs.rust-bin.stable."1.85.0".default.override {
  60. targets = [ "wasm32-unknown-unknown" ]; # wasm
  61. extensions = [
  62. "rustfmt"
  63. "clippy"
  64. "rust-analyzer"
  65. ];
  66. };
  67. # Nightly used for formatting
  68. nightly_toolchain = pkgs.rust-bin.selectLatestNightlyWith (
  69. toolchain:
  70. toolchain.default.override {
  71. extensions = [
  72. "rustfmt"
  73. "clippy"
  74. "rust-analyzer"
  75. "rust-src"
  76. ];
  77. targets = [ "wasm32-unknown-unknown" ]; # wasm
  78. }
  79. );
  80. # Common inputs
  81. envVars = {
  82. # rust analyzer needs NIX_PATH for some reason.
  83. NIX_PATH = "nixpkgs=${inputs.nixpkgs}";
  84. };
  85. buildInputs =
  86. with pkgs;
  87. [
  88. # Add additional build inputs here
  89. git
  90. pkg-config
  91. curl
  92. just
  93. protobuf
  94. nixpkgs-fmt
  95. typos
  96. lnd
  97. clightning
  98. bitcoind
  99. sqlx-cli
  100. mprocs
  101. cargo-outdated
  102. cargo-mutants
  103. # Needed for github ci
  104. libz
  105. ]
  106. ++ libsDarwin;
  107. # PostgreSQL configuration
  108. postgresConf = {
  109. pgUser = "cdk_user";
  110. pgPassword = "cdk_password";
  111. pgDatabase = "cdk_mint";
  112. pgPort = "5432";
  113. };
  114. # Script to start PostgreSQL
  115. startPostgres = pkgs.writeShellScriptBin "start-postgres" ''
  116. set -e
  117. PGDATA="$PWD/.pg_data"
  118. PGPORT="${postgresConf.pgPort}"
  119. PGUSER="${postgresConf.pgUser}"
  120. PGPASSWORD="${postgresConf.pgPassword}"
  121. PGDATABASE="${postgresConf.pgDatabase}"
  122. # Stop any existing instance first
  123. if [ -d "$PGDATA" ] && ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" status > /dev/null 2>&1; then
  124. echo "Stopping existing PostgreSQL instance..."
  125. ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" stop > /dev/null 2>&1
  126. fi
  127. if [ ! -d "$PGDATA" ]; then
  128. echo "Initializing PostgreSQL database..."
  129. ${pkgs.postgresql_16}/bin/initdb -D "$PGDATA" --auth=trust --no-locale --encoding=UTF8
  130. # Configure PostgreSQL
  131. echo "listen_addresses = 'localhost'" >> "$PGDATA/postgresql.conf"
  132. echo "port = $PGPORT" >> "$PGDATA/postgresql.conf"
  133. echo "unix_socket_directories = '$PGDATA'" >> "$PGDATA/postgresql.conf"
  134. # Start temporarily to create user and database
  135. ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" -l "$PGDATA/logfile" start
  136. sleep 2
  137. # Create user and database
  138. ${pkgs.postgresql_16}/bin/createuser -h localhost -p $PGPORT -s "$PGUSER" || true
  139. ${pkgs.postgresql_16}/bin/psql -h localhost -p $PGPORT -c "ALTER USER $PGUSER WITH PASSWORD '$PGPASSWORD';" postgres
  140. ${pkgs.postgresql_16}/bin/createdb -h localhost -p $PGPORT -O "$PGUSER" "$PGDATABASE" || true
  141. ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" stop
  142. echo "PostgreSQL initialized."
  143. fi
  144. echo "Starting PostgreSQL on port $PGPORT..."
  145. ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" -l "$PGDATA/logfile" start
  146. echo "PostgreSQL started. Connection URL: postgresql://$PGUSER:$PGPASSWORD@localhost:$PGPORT/$PGDATABASE"
  147. '';
  148. # Script to stop PostgreSQL
  149. stopPostgres = pkgs.writeShellScriptBin "stop-postgres" ''
  150. PGDATA="$PWD/.pg_data"
  151. if [ -d "$PGDATA" ]; then
  152. echo "Stopping PostgreSQL..."
  153. ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" stop || echo "PostgreSQL was not running."
  154. else
  155. echo "No PostgreSQL data directory found."
  156. fi
  157. '';
  158. # Script to check PostgreSQL status
  159. pgStatus = pkgs.writeShellScriptBin "pg-status" ''
  160. PGDATA="$PWD/.pg_data"
  161. if [ -d "$PGDATA" ]; then
  162. ${pkgs.postgresql_16}/bin/pg_ctl -D "$PGDATA" status
  163. else
  164. echo "No PostgreSQL data directory found. Run 'start-postgres' first."
  165. fi
  166. '';
  167. # Script to connect to PostgreSQL
  168. pgConnect = pkgs.writeShellScriptBin "pg-connect" ''
  169. ${pkgs.postgresql_16}/bin/psql "postgresql://${postgresConf.pgUser}:${postgresConf.pgPassword}@localhost:${postgresConf.pgPort}/${postgresConf.pgDatabase}"
  170. '';
  171. # Common arguments can be set here to avoid repeating them later
  172. nativeBuildInputs = [
  173. #Add additional build inputs here
  174. ]
  175. ++ lib.optionals isDarwin [
  176. # Additional darwin specific native inputs can be set here
  177. ];
  178. in
  179. {
  180. checks = {
  181. # Pre-commit checks
  182. pre-commit-check =
  183. let
  184. # this is a hack based on https://github.com/cachix/pre-commit-hooks.nix/issues/126
  185. # we want to use our own rust stuff from oxalica's overlay
  186. _rust = pkgs.rust-bin.stable.latest.default;
  187. rust = pkgs.buildEnv {
  188. name = _rust.name;
  189. inherit (_rust) meta;
  190. buildInputs = [ pkgs.makeWrapper ];
  191. paths = [ _rust ];
  192. pathsToLink = [
  193. "/"
  194. "/bin"
  195. ];
  196. postBuild = ''
  197. for i in $out/bin/*; do
  198. wrapProgram "$i" --prefix PATH : "$out/bin"
  199. done
  200. '';
  201. };
  202. in
  203. pre-commit-hooks.lib.${system}.run {
  204. src = ./.;
  205. hooks = {
  206. rustfmt = {
  207. enable = true;
  208. entry = lib.mkForce "${rust}/bin/cargo-fmt fmt --all -- --config format_code_in_doc_comments=true --check --color always";
  209. };
  210. nixpkgs-fmt.enable = true;
  211. typos.enable = true;
  212. commitizen.enable = true; # conventional commits
  213. };
  214. };
  215. };
  216. devShells =
  217. let
  218. # pre-commit-checks
  219. _shellHook = (self.checks.${system}.pre-commit-check.shellHook or "");
  220. # devShells
  221. msrv = pkgs.mkShell (
  222. {
  223. shellHook = "
  224. cargo update
  225. cargo update home --precise 0.5.11
  226. ${_shellHook}
  227. ";
  228. buildInputs = buildInputs ++ [ msrv_toolchain ];
  229. inherit nativeBuildInputs;
  230. }
  231. // envVars
  232. );
  233. stable = pkgs.mkShell (
  234. {
  235. shellHook = ''
  236. ${_shellHook}
  237. # Needed for github ci
  238. export LD_LIBRARY_PATH=${
  239. pkgs.lib.makeLibraryPath [
  240. pkgs.zlib
  241. ]
  242. }:$LD_LIBRARY_PATH
  243. # PostgreSQL environment variables
  244. export CDK_MINTD_DATABASE_URL="postgresql://${postgresConf.pgUser}:${postgresConf.pgPassword}@localhost:${postgresConf.pgPort}/${postgresConf.pgDatabase}"
  245. echo ""
  246. echo "PostgreSQL commands available:"
  247. echo " start-postgres - Initialize and start PostgreSQL"
  248. echo " stop-postgres - Stop PostgreSQL (run before exiting)"
  249. echo " pg-status - Check PostgreSQL status"
  250. echo " pg-connect - Connect to PostgreSQL with psql"
  251. echo ""
  252. '';
  253. buildInputs = buildInputs ++ [
  254. stable_toolchain
  255. pkgs.postgresql_16
  256. startPostgres
  257. stopPostgres
  258. pgStatus
  259. pgConnect
  260. ];
  261. inherit nativeBuildInputs;
  262. }
  263. // envVars
  264. );
  265. nightly = pkgs.mkShell (
  266. {
  267. shellHook = ''
  268. ${_shellHook}
  269. # Needed for github ci
  270. export LD_LIBRARY_PATH=${
  271. pkgs.lib.makeLibraryPath [
  272. pkgs.zlib
  273. ]
  274. }:$LD_LIBRARY_PATH
  275. '';
  276. buildInputs = buildInputs ++ [ nightly_toolchain ];
  277. inherit nativeBuildInputs;
  278. }
  279. // envVars
  280. );
  281. # Shell with Docker for integration tests
  282. integration = pkgs.mkShell (
  283. {
  284. shellHook = ''
  285. ${_shellHook}
  286. # Ensure Docker is available
  287. if ! command -v docker &> /dev/null; then
  288. echo "Docker is not installed or not in PATH"
  289. echo "Please install Docker to run integration tests"
  290. exit 1
  291. fi
  292. echo "Docker is available at $(which docker)"
  293. echo "Docker version: $(docker --version)"
  294. '';
  295. buildInputs = buildInputs ++ [
  296. stable_toolchain
  297. pkgs.docker-client
  298. pkgs.python311
  299. ];
  300. inherit nativeBuildInputs;
  301. }
  302. // envVars
  303. );
  304. in
  305. {
  306. inherit
  307. msrv
  308. stable
  309. nightly
  310. integration
  311. ;
  312. default = stable;
  313. };
  314. }
  315. );
  316. }