diff --git a/.gitignore b/.gitignore index 02200a3..1ac43fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /themes +/result diff --git a/build.rs b/build.rs index 142ea61..675773b 100644 --- a/build.rs +++ b/build.rs @@ -1,15 +1,33 @@ -use std::fs::{File, create_dir}; -use std::path::Path; +use std::fs::File; +use std::path::PathBuf; + +// This file is kind of ugly, I tried to make nix build work fn main() { - let dest_path = Path::new("themes/Catppuccin-Macchiato.tmTheme"); + let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); + let local_theme_path = PathBuf::from(&manifest_dir).join("themes/Catppuccin-Macchiato.tmTheme"); - if !dest_path.exists() { - let mut response = reqwest::blocking::get("https://raw.githubusercontent.com/catppuccin/bat/refs/heads/main/themes/Catppuccin%20Macchiato.tmTheme") - .expect("Failed to download theme"); + let final_path = if let Ok(nix_theme_path) = std::env::var("THEME_PATH") { + // In case of nix build + println!("cargo:rerun-if-env-changed=THEME_PATH"); + nix_theme_path + } else { + // In case of cargo + if !local_theme_path.exists() { + println!("cargo:warning=Theme not found, downloading..."); - create_dir("themes").expect("Could not create themes dir"); - let mut file = File::create(dest_path).expect("Failed to create theme file"); - std::io::copy(&mut response, &mut file).expect("Failed to save theme"); - } + std::fs::create_dir_all(local_theme_path.parent().unwrap()) + .expect("Could not create themes directory"); + + let mut response = reqwest::blocking::get("https://raw.githubusercontent.com/catppuccin/bat/refs/heads/main/themes/Catppuccin%20Macchiato.tmTheme") + .expect("Failed to download theme"); + + let mut file = File::create(&local_theme_path).expect("Failed to create theme file"); + std::io::copy(&mut response, &mut file).expect("Failed to save theme"); + } + + local_theme_path.to_str().unwrap().to_string() + }; + + println!("cargo:rustc-env=THEME_FILE_PATH={}", final_path); } diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..39feaa9 --- /dev/null +++ b/flake.lock @@ -0,0 +1,109 @@ +{ + "nodes": { + "code-theme": { + "flake": false, + "locked": { + "narHash": "sha256-28LGXKITHbrmt6qrcG/W+qTlaWthre7x7izp/TPjQgA=", + "type": "file", + "url": "https://raw.githubusercontent.com/catppuccin/bat/refs/heads/main/themes/Catppuccin%20Macchiato.tmTheme" + }, + "original": { + "type": "file", + "url": "https://raw.githubusercontent.com/catppuccin/bat/refs/heads/main/themes/Catppuccin%20Macchiato.tmTheme" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1767767207, + "narHash": "sha256-Mj3d3PfwltLmukFal5i3fFt27L6NiKXdBezC1EBuZs4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5912c1772a44e31bf1c63c0390b90501e5026886", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1744536153, + "narHash": "sha256-awS2zRgF4uTwrOKwwiJcByDzDOdo3Q1rPZbiHQg/N38=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "18dd725c29603f582cf1900e0d25f9f1063dbf11", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "code-theme": "code-theme", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1767754000, + "narHash": "sha256-znoNJs2QZFl+wCFLd6FbUJ00c74kvzOjyQYXc45uFvo=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "0b3a5ad260479f2c9bdadf3ba5b2a4be359cfcdd", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..228d350 --- /dev/null +++ b/flake.nix @@ -0,0 +1,64 @@ +{ + description = "Blog website"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + rust-overlay.url = "github:oxalica/rust-overlay"; + + code-theme = { + url = "https://raw.githubusercontent.com/catppuccin/bat/refs/heads/main/themes/Catppuccin%20Macchiato.tmTheme"; + flake = false; + }; + }; + + outputs = { nixpkgs, flake-utils, rust-overlay, code-theme, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + overlays = [ rust-overlay.overlays.default ]; + pkgs = import nixpkgs { + inherit system overlays; + }; + rust = pkgs.rust-bin.stable.latest.default; + openssl = pkgs.openssl; + in + { + packages.default = pkgs.rustPlatform.buildRustPackage { + pname = "blog"; + version = "0.1.0"; + + src = ./.; + + THEME_PATH = "${code-theme}"; + + cargoLock = { + lockFile = ./Cargo.lock; + }; + + nativeBuildInputs = [ + rust + pkgs.pkg-config + ]; + + buildInputs = [ + openssl + ]; + + OPENSSL_LIB_DIR = "${openssl.out}/lib"; + OPENSSL_INCLUDE_DIR = "${openssl.dev}/include"; + PKG_CONFIG_PATH = "${openssl.dev}/lib/pkgconfig"; + }; + + devShells.default = pkgs.mkShell { + packages = [ + rust + pkgs.cargo + pkgs.rust-analyzer + pkgs.pkg-config + openssl + ]; + OPENSSL_DIR = openssl.dev; + PKG_CONFIG_PATH = "${openssl.dev}/lib/pkgconfig"; + }; + }); +} diff --git a/src/main.rs b/src/main.rs index 5c78149..a51071e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,8 @@ lazy_static! { pub static ref THEME_SET: ThemeSet = { let mut set = ThemeSet::load_defaults(); - let theme_bytes = include_bytes!("../themes/Catppuccin-Macchiato.tmTheme"); + // let theme_bytes = include_bytes!("../themes/Catppuccin-Macchiato.tmTheme"); + let theme_bytes = include_bytes!(env!("THEME_FILE_PATH")); let mut cursor = Cursor::new(theme_bytes); match syntect::highlighting::ThemeSet::load_from_reader(&mut cursor) {