Highly ambitious ATProtocol AppView service and sdks
at main 5.4 kB view raw
1{ 2 description = "API service for Slices"; 3 4 inputs = { 5 nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; 6 crane.url = "github:ipetkov/crane"; 7 fenix = { 8 url = "github:nix-community/fenix"; 9 inputs.nixpkgs.follows = "nixpkgs"; 10 inputs.rust-analyzer-src.follows = ""; 11 }; 12 }; 13 14 outputs = { self, nixpkgs, crane, fenix }: 15 let 16 systems = [ "x86_64-linux" "aarch64-darwin" ]; 17 forAllSystems = nixpkgs.lib.genAttrs systems; 18 19 mkPackagesForSystem = system: 20 let 21 pkgs = import nixpkgs { 22 inherit system; 23 config = { allowUnfree = true; }; 24 }; 25 26 # Configure crane with stable Rust toolchain from nixpkgs (more reliable) 27 craneLib = crane.mkLib pkgs; 28 29 # Project source for crane 30 src = pkgs.lib.cleanSourceWith { 31 src = ../.; 32 filter = path: type: 33 (craneLib.filterCargoSources path type) || 34 (pkgs.lib.hasInfix "/migrations/" path) || 35 (pkgs.lib.hasSuffix "/migrations" path) || 36 (pkgs.lib.hasInfix "/.sqlx/" path) || 37 (pkgs.lib.hasSuffix "/.sqlx" path) || 38 (pkgs.lib.hasInfix "/scripts/" path) || 39 (pkgs.lib.hasSuffix "/scripts" path) || 40 (pkgs.lib.hasInfix "/packages/lexicon-rs/" path); 41 }; 42 43 commonArgs = { 44 inherit src; 45 version = "0.1.0"; 46 strictDeps = true; 47 pname = "slices"; 48 name = "slices"; 49 sourceRoot = "source/api"; 50 cargoLock = ../api/Cargo.lock; 51 buildInputs = with pkgs; [ 52 openssl 53 pkg-config 54 postgresql 55 stdenv.cc.cc.lib 56 ] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [ 57 glibc 58 ]; 59 nativeBuildInputs = with pkgs; [ 60 pkg-config 61 openssl.dev 62 sqlx-cli 63 stdenv.cc 64 ]; 65 66 # Environment variables for OpenSSL 67 OPENSSL_NO_VENDOR = 1; 68 PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig"; 69 70 # Environment variables for SQLx 71 SQLX_OFFLINE = "true"; 72 73 # Fix for linker issues in Nix 74 CC = "${pkgs.stdenv.cc}/bin/cc"; 75 76 cargoExtraArgs = "--bin slices"; 77 }; 78 79 # Build cargo artifacts 80 cargoArtifacts = craneLib.buildDepsOnly commonArgs; 81 82 # Build the package 83 slices = craneLib.buildPackage (commonArgs // { 84 cargoArtifacts = cargoArtifacts; 85 doCheck = false; 86 CARGO_PROFILE = "release"; 87 }); 88 89 # Copy migration files 90 migrationFiles = pkgs.stdenv.mkDerivation { 91 name = "slices-migrations"; 92 src = ../api/migrations; 93 installPhase = '' 94 mkdir -p $out/migrations 95 cp -r * $out/migrations/ 96 ''; 97 }; 98 99 # Copy script files 100 scriptFiles = pkgs.stdenv.mkDerivation { 101 name = "slices-scripts"; 102 src = ../api/scripts; 103 installPhase = '' 104 mkdir -p $out/scripts 105 cp -r * $out/scripts/ 106 ''; 107 }; 108 109 110 # Common OCI labels 111 ociLabels = { 112 "org.opencontainers.image.title" = "slices"; 113 "org.opencontainers.image.description" = "API service for Slices"; 114 "org.opencontainers.image.version" = "0.1.0"; 115 "org.opencontainers.image.authors" = "Slices Social"; 116 "org.opencontainers.image.licenses" = "MIT"; 117 }; 118 119 # Docker image for deployment 120 slicesImg = pkgs.dockerTools.buildImage { 121 name = "slices"; 122 tag = "latest"; 123 fromImage = pkgs.dockerTools.pullImage { 124 imageName = "alpine"; 125 imageDigest = "sha256:beefdbd8a1da6d2915566fde36db9db0b524eb737fc57cd1367effd16dc0d06d"; 126 sha256 = "sha256-Sfb0quuaHgzxA7paz5P51WhdA35to39HtOufceXixz0="; 127 }; 128 copyToRoot = pkgs.buildEnv { 129 name = "image-root"; 130 paths = [ 131 slices 132 pkgs.cacert 133 pkgs.postgresql 134 scriptFiles 135 ]; 136 pathsToLink = [ "/bin" "/etc" "/scripts" ]; 137 }; 138 139 config = { 140 Cmd = [ "/bin/slices" ]; 141 Env = [ 142 "RUST_BACKTRACE=1" 143 "RUST_LOG=info" 144 "PORT=8080" 145 ]; 146 ExposedPorts = { 147 "8080/tcp" = {}; 148 }; 149 Labels = ociLabels; 150 }; 151 }; 152 153 in 154 { 155 inherit slices slicesImg; 156 default = slices; 157 }; 158 in 159 { 160 packages = forAllSystems mkPackagesForSystem; 161 162 devShells = forAllSystems (system: 163 let 164 pkgs = import nixpkgs { inherit system; }; 165 craneLib = crane.mkLib pkgs; 166 in 167 { 168 default = craneLib.devShell { 169 packages = with pkgs; [ 170 nixpkgs-fmt 171 nil 172 dive 173 postgresql 174 sqlx-cli 175 ]; 176 177 # Set up environment for development 178 RUST_LOG = "info"; 179 }; 180 }); 181 }; 182}