grain.social is a photo sharing platform built on atproto.
1{
2 description = "Darkroom service for Grain";
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" ];
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
27 craneLib = (crane.mkLib pkgs).overrideToolchain
28 fenix.packages.${system}.stable.toolchain;
29
30 # Project source for crane
31 src = pkgs.lib.cleanSourceWith {
32 src = ./.;
33 filter = path: type:
34 (craneLib.filterCargoSources path type) ||
35 # Include templates and static directories for include_str! macros
36 (pkgs.lib.hasInfix "/templates/" path) ||
37 (pkgs.lib.hasInfix "/static/" path) ||
38 (pkgs.lib.hasSuffix "/templates" path) ||
39 (pkgs.lib.hasSuffix "/static" path);
40 };
41
42 commonArgs = {
43 inherit src;
44 version = "0.1.0";
45 strictDeps = true;
46 pname = "darkroom";
47 name = "darkroom";
48 buildInputs = with pkgs; [
49 openssl
50 pkg-config
51 ];
52 nativeBuildInputs = with pkgs; [
53 pkg-config
54 openssl.dev
55 ];
56
57 # Environment variables for OpenSSL
58 OPENSSL_NO_VENDOR = 1;
59 PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
60 };
61
62 cargoArtifacts = craneLib.buildDepsOnly commonArgs;
63
64 darkroom = craneLib.buildPackage (commonArgs // {
65 inherit cargoArtifacts;
66 doCheck = false;
67 CARGO_PROFILE = "release";
68 });
69
70 # Docker image for deployment
71 darkroomImg = let
72 fontDirs = [
73 "${pkgs.corefonts}/share/fonts"
74 "${pkgs.dejavu_fonts}/share/fonts"
75 "${pkgs.liberation_ttf}/share/fonts"
76 "${pkgs.noto-fonts}/share/fonts"
77 "${pkgs.noto-fonts-color-emoji}/share/fonts"
78 ];
79 fontsConf = pkgs.makeFontsConf { fontDirectories = fontDirs; };
80 in pkgs.dockerTools.buildImage {
81 name = "darkroom";
82 tag = "latest";
83 contents = [
84 darkroom
85 pkgs.chromium
86 pkgs.chromedriver
87 pkgs.cacert
88 pkgs.corefonts
89 pkgs.dejavu_fonts
90 pkgs.liberation_ttf
91 pkgs.noto-fonts
92 pkgs.noto-fonts-color-emoji
93 ];
94
95 runAsRoot = ''
96 #!${pkgs.runtimeShell}
97 mkdir -p /tmp /app/chrome-profile
98 chmod 1777 /tmp
99 chmod 755 /app/chrome-profile
100 '';
101
102 config = {
103 Cmd = [ "/bin/darkroom" ];
104 Env = [
105 "RUST_BACKTRACE=1"
106 "RUST_LOG=debug"
107 "CHROME_PATH=${pkgs.chromium}/bin/chromium"
108 "CHROMEDRIVER_PATH=${pkgs.chromedriver}/bin/chromedriver"
109 "BASE_URL=http://grain-darkroom.internal:8080"
110 "GRAIN_BASE_URL=https://grain.social"
111 "PORT=8080"
112 "FONTCONFIG_FILE=${fontsConf}"
113 ];
114 ExposedPorts = {
115 "8080/tcp" = {};
116 };
117 };
118 };
119 in
120 {
121 inherit darkroom darkroomImg;
122 default = darkroom;
123 };
124 in
125 {
126 packages = forAllSystems mkPackagesForSystem;
127
128 devShells = forAllSystems (system:
129 let
130 pkgs = import nixpkgs { inherit system; };
131 craneLib = (crane.mkLib pkgs).overrideToolchain
132 fenix.packages.${system}.stable.toolchain;
133 in
134 {
135 default = craneLib.devShell {
136 packages = with pkgs; [
137 nixpkgs-fmt
138 nil
139 dive
140 flyctl
141 chromium
142 ];
143
144 # Set up environment for development
145 RUST_LOG = "debug";
146 CHROME_PATH = "${pkgs.chromium}/bin/chromium";
147 };
148 });
149 };
150}