diff --git a/README.md b/README.md index 9c73f34b..d594e1dd 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,11 @@ yay -S sqlit # Nix (flake) nix run github:Maxteabag/sqlit + +# Nix with specific optional dependencies +# See docs/nix-flake.md for detailed configuration options +nix run github:Maxteabag/sqlit#sqlit-minimal # SQLite only +nix run github:Maxteabag/sqlit#sqlit-full # All dependencies ``` ## Usage diff --git a/docs/nix-flake.md b/docs/nix-flake.md new file mode 100644 index 00000000..cb9c2131 --- /dev/null +++ b/docs/nix-flake.md @@ -0,0 +1,158 @@ +# Nix Flake Usage + +This document describes how to use the Nix flake with optional dependencies. + +## Overview + +The sqlit Nix flake provides flexible package variants with optional database driver and SSH tunnel support. You can choose between pre-configured variants or create custom builds with exactly the dependencies you need. + +## Pre-configured Variants + +### Default Package (Recommended) +The default package includes common database drivers and SSH support: +```bash +nix run github:Maxteabag/sqlit +# or +nix profile install github:Maxteabag/sqlit +``` + +**Included dependencies:** +- SSH tunnel support (sshtunnel, paramiko) +- PostgreSQL driver (psycopg2) +- MySQL driver (pymysql) +- DuckDB driver (duckdb) + +### Minimal Package +For users who only need SQLite support: +```bash +nix run github:Maxteabag/sqlit#sqlit-minimal +# or +nix profile install github:Maxteabag/sqlit#sqlit-minimal +``` + +**Included dependencies:** +- Only core dependencies (no optional drivers) +- SQLite support (built-in to Python) + +### Full Package +For users who want all available dependencies: +```bash +nix run github:Maxteabag/sqlit#sqlit-full +# or +nix profile install github:Maxteabag/sqlit#sqlit-full +``` + +**Included dependencies:** +- All database drivers available in nixpkgs +- SSH tunnel support +- All cloud database connectors + +## Custom Builds + +You can create a custom build with exactly the dependencies you need by creating a `flake.nix` in your project. + +**See `examples/custom-flake.nix` for a complete working example.** + +Basic structure: + +```nix +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + sqlit.url = "github:Maxteabag/sqlit"; + }; + + outputs = { self, nixpkgs, sqlit }: + let + system = "x86_64-linux"; # or your system + pkgs = nixpkgs.legacyPackages.${system}; + in { + packages.${system}.default = sqlit.lib.${system}.makeSqlit { + # Enable only what you need + enableSSH = true; + enablePostgres = true; + enableMySQL = false; + enableMSSQL = false; + enableDuckDB = true; + # ... other options + }; + }; +} +``` + +Then run: +```bash +nix run +``` + +## Available Options + +All options default to sensible values for common use cases. Set to `true` to enable or `false` to disable: + +### SSH Tunnel Support +- `enableSSH` (default: `true`) - Enables SSH tunnel connections + +### Popular Database Drivers +- `enablePostgres` (default: `true`) - PostgreSQL, CockroachDB, Supabase +- `enableMySQL` (default: `true`) - MySQL support +- `enableMSSQL` (default: `false`) - SQL Server support (not in nixpkgs, placeholder) +- `enableDuckDB` (default: `true`) - DuckDB analytics database + +### Advanced Database Drivers +- `enableOracle` (default: `false`) - Oracle Database (not in nixpkgs) +- `enableMariaDB` (default: `false`) - MariaDB (not in nixpkgs) +- `enableDB2` (default: `false`) - IBM Db2 (not in nixpkgs) +- `enableHANA` (default: `false`) - SAP HANA (not in nixpkgs) +- `enableTeradata` (default: `false`) - Teradata (not in nixpkgs) +- `enableTrino` (default: `false`) - Trino (not in nixpkgs) +- `enablePresto` (default: `false`) - Presto (not in nixpkgs) + +### Cloud and Modern Databases +- `enableBigQuery` (default: `false`) - Google BigQuery +- `enableRedshift` (default: `false`) - AWS Redshift (not in nixpkgs) +- `enableClickHouse` (default: `false`) - ClickHouse (not in nixpkgs) +- `enableCloudflareD1` (default: `false`) - Cloudflare D1 +- `enableTurso` (default: `false`) - Turso (not in nixpkgs) +- `enableFirebird` (default: `false`) - Firebird SQL (not in nixpkgs) +- `enableSnowflake` (default: `false`) - Snowflake +- `enableAthena` (default: `false`) - AWS Athena (not in nixpkgs) +- `enableFlightSQL` (default: `false`) - Apache Arrow Flight SQL (not in nixpkgs) + +### Enable All +- `enableAll` (default: `false`) - Enable all available optional dependencies + +## Note on Nixpkgs Availability + +Some Python packages are not yet available in nixpkgs. These are marked with comments in the flake and will be skipped during build. If you need these drivers, you can: + +1. Use `pipx inject` or `pip install` after installation +2. Use a Python virtual environment alongside the Nix installation +3. Contribute the missing packages to nixpkgs + +## Examples + +### PostgreSQL and MySQL only +```nix +sqlit.lib.${system}.makeSqlit { + enableSSH = true; + enablePostgres = true; + enableMySQL = true; + enableDuckDB = false; +} +``` + +### Cloud databases +```nix +sqlit.lib.${system}.makeSqlit { + enableBigQuery = true; + enableSnowflake = true; + enableAthena = true; +} +``` + +### Everything available +```nix +sqlit.lib.${system}.makeSqlit { + enableAll = true; +} +``` diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..0c5cfefc --- /dev/null +++ b/examples/README.md @@ -0,0 +1,60 @@ +# Nix Flake Examples + +This directory contains example configurations for building custom sqlit variants with the Nix flake. + +## custom-flake.nix + +An example showing how to create a custom sqlit build with only the database drivers you need. + +This example enables: +- SSH tunnel support +- PostgreSQL driver +- MySQL driver + +And disables all other optional dependencies. + +### How to use: + +1. Copy the file to your project: + ```bash + cp examples/custom-flake.nix your-project/flake.nix + ``` + +2. Modify the parameters to suit your needs: + - Set `enableSSH = true` if you need SSH tunnel support + - Enable the database drivers you need (e.g., `enablePostgres = true`) + - Disable the ones you don't need (e.g., `enableOracle = false`) + +3. Run your custom sqlit: + ```bash + cd your-project + nix run + ``` + +### Available Parameters + +See `docs/nix-flake.md` for a complete list of all available parameters and their defaults. + +### Quick Reference + +Common database parameters: +- `enableSSH` - SSH tunnel support +- `enablePostgres` - PostgreSQL, CockroachDB, Supabase +- `enableMySQL` - MySQL +- `enableMSSQL` - SQL Server +- `enableDuckDB` - DuckDB analytics database +- `enableOracle` - Oracle Database +- `enableBigQuery` - Google BigQuery +- `enableSnowflake` - Snowflake +- `enableAll` - Enable all available dependencies + +## Tips + +1. **Start minimal, add what you need**: Begin with all options disabled, then enable only what you need. + +2. **Check nixpkgs availability**: Some Python packages are not in nixpkgs. See `docs/nix-flake.md` for details. + +3. **Use pre-configured variants for common cases**: If you just need common databases, use the default package instead: + ```bash + nix run github:Maxteabag/sqlit + ``` diff --git a/examples/custom-flake.nix b/examples/custom-flake.nix new file mode 100644 index 00000000..cc334355 --- /dev/null +++ b/examples/custom-flake.nix @@ -0,0 +1,60 @@ +# Example: Custom sqlit build with specific dependencies +# +# This is an example flake.nix that shows how to create a custom +# sqlit build with exactly the dependencies you need. +# +# To use this: +# 1. Copy this file to your project directory as `flake.nix` +# 2. Modify the makeSqlit parameters to enable/disable what you need +# 3. Run: nix run + +{ + description = "Custom sqlit build"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + sqlit.url = "github:Maxteabag/sqlit"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, sqlit, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + # Create a custom sqlit with only PostgreSQL and MySQL support + my-sqlit = sqlit.lib.${system}.makeSqlit { + # Enable SSH tunneling + enableSSH = true; + + # Enable only PostgreSQL and MySQL + enablePostgres = true; + enableMySQL = true; + + # Disable everything else + enableMSSQL = false; + enableDuckDB = false; + enableOracle = false; + enableMariaDB = false; + enableDB2 = false; + enableHANA = false; + enableTeradata = false; + enableTrino = false; + enablePresto = false; + enableBigQuery = false; + enableRedshift = false; + enableClickHouse = false; + enableCloudflareD1 = false; + enableTurso = false; + enableFirebird = false; + enableSnowflake = false; + enableAthena = false; + enableFlightSQL = false; + }; + in { + packages.default = my-sqlit; + + apps.default = { + type = "app"; + program = "${my-sqlit}/bin/sqlit"; + }; + }); +} diff --git a/flake.nix b/flake.nix index db6be541..87854094 100644 --- a/flake.nix +++ b/flake.nix @@ -13,22 +13,111 @@ lib = pkgs.lib; pyPkgs = pkgs.python3.pkgs; - ref = - if self ? sourceInfo && self.sourceInfo ? ref - then self.sourceInfo.ref - else ""; - tag = - if lib.hasPrefix "refs/tags/v" ref - then lib.removePrefix "refs/tags/v" ref - else if lib.hasPrefix "refs/tags/" ref - then lib.removePrefix "refs/tags/" ref - else if lib.hasPrefix "v" ref - then lib.removePrefix "v" ref - else ""; - shortRev = if self ? shortRev then self.shortRev else "dirty"; - version = if tag != "" then tag else "0.0.0+${shortRev}"; - - sqlit = pyPkgs.buildPythonApplication { + # Helper function to build sqlit with optional dependencies + makeSqlit = { + # SSH tunnel support + enableSSH ? true, + # Popular database drivers + enablePostgres ? true, + enableMySQL ? true, + enableMSSQL ? false, + # Advanced database drivers + enableOracle ? false, + enableMariaDB ? false, + enableDB2 ? false, + enableHANA ? false, + enableTeradata ? false, + enableTrino ? false, + enablePresto ? false, + # Cloud and modern databases + enableBigQuery ? false, + enableRedshift ? false, + enableDuckDB ? true, + enableClickHouse ? false, + enableCloudflareD1 ? false, + enableTurso ? false, + enableFirebird ? false, + enableSnowflake ? false, + enableAthena ? false, + enableFlightSQL ? false, + # Enable all optional dependencies + enableAll ? false, + }: + + let + ref = + if self ? sourceInfo && self.sourceInfo ? ref + then self.sourceInfo.ref + else ""; + tag = + if lib.hasPrefix "refs/tags/v" ref + then lib.removePrefix "refs/tags/v" ref + else if lib.hasPrefix "refs/tags/" ref + then lib.removePrefix "refs/tags/" ref + else if lib.hasPrefix "v" ref + then lib.removePrefix "v" ref + else ""; + shortRev = if self ? shortRev then self.shortRev else "dirty"; + version = if tag != "" then tag else "0.0.0+${shortRev}"; + + # Determine if we should enable each dependency + withSSH = enableAll || enableSSH; + withPostgres = enableAll || enablePostgres; + withMySQL = enableAll || enableMySQL; + withMSSQL = enableAll || enableMSSQL; + withOracle = enableAll || enableOracle; + withMariaDB = enableAll || enableMariaDB; + withDB2 = enableAll || enableDB2; + withHANA = enableAll || enableHANA; + withTeradata = enableAll || enableTeradata; + withTrino = enableAll || enableTrino; + withPresto = enableAll || enablePresto; + withBigQuery = enableAll || enableBigQuery; + withRedshift = enableAll || enableRedshift; + withDuckDB = enableAll || enableDuckDB; + withClickHouse = enableAll || enableClickHouse; + withCloudflareD1 = enableAll || enableCloudflareD1; + withTurso = enableAll || enableTurso; + withFirebird = enableAll || enableFirebird; + withSnowflake = enableAll || enableSnowflake; + withAthena = enableAll || enableAthena; + withFlightSQL = enableAll || enableFlightSQL; + + # Build list of optional dependencies based on flags + # Note: Some packages are not available in nixpkgs and are documented here: + # - mssql-python (SQL Server) + # - oracledb (Oracle) + # - mariadb (MariaDB) + # - ibm_db (DB2) + # - hdbcli (SAP HANA) + # - teradatasql (Teradata) + # - trino (Trino) + # - presto-python-client (Presto) + # - redshift-connector (Redshift) + # - clickhouse-connect (ClickHouse) + # - libsql (Turso) + # - firebirdsql (Firebird) + # - pyathena (Athena) + # - adbc-driver-flightsql (Apache Arrow Flight SQL) + # Users needing these can install via pipx inject or pip after installation + + optionalDeps = lib.optionals withSSH [ + pyPkgs.sshtunnel + pyPkgs.paramiko + ] ++ lib.optionals withPostgres [ + pyPkgs.psycopg2 + ] ++ lib.optionals withMySQL [ + pyPkgs.pymysql + ] ++ lib.optionals withBigQuery [ + pyPkgs.google-cloud-bigquery + ] ++ lib.optionals withDuckDB [ + pyPkgs.duckdb + ] ++ lib.optionals withCloudflareD1 [ + pyPkgs.requests + ] ++ lib.optionals withSnowflake [ + pyPkgs.snowflake-connector-python + ]; + in pyPkgs.buildPythonApplication { pname = "sqlit"; inherit version; pyproject = true; @@ -58,7 +147,7 @@ pyPkgs.sqlparse pyPkgs.textual pyPkgs."textual-fastdatatable" - ]; + ] ++ optionalDeps; pythonImportsCheck = [ "sqlit" ]; @@ -69,12 +158,31 @@ mainProgram = "sqlit"; }; }; + + # Default sqlit with common options enabled + sqlit = makeSqlit {}; + + # Minimal sqlit with only built-in SQLite support + sqlit-minimal = makeSqlit { + enableSSH = false; + enablePostgres = false; + enableMySQL = false; + enableDuckDB = false; + }; + + # Full-featured sqlit with all available dependencies + sqlit-full = makeSqlit { + enableAll = true; + }; in { packages = { - inherit sqlit; + inherit sqlit sqlit-minimal sqlit-full; default = sqlit; }; + # Expose the makeSqlit function so users can create custom variants + lib.makeSqlit = makeSqlit; + apps.default = { type = "app"; program = "${sqlit}/bin/sqlit"; diff --git a/test-nix-flake.sh b/test-nix-flake.sh new file mode 100755 index 00000000..285d1090 --- /dev/null +++ b/test-nix-flake.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash +# Test script for Nix flake optional dependencies +# This script requires Nix to be installed +# +# Usage: +# chmod +x test-nix-flake.sh # Make executable (first time only) +# ./test-nix-flake.sh # Run tests + +set -e + +echo "Testing sqlit Nix flake with optional dependencies..." +echo "" + +# Check if nix is available +if ! command -v nix &> /dev/null; then + echo "❌ Nix is not installed. Please install Nix first:" + echo " curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install" + exit 1 +fi + +echo "✓ Nix is installed" +echo "" + +# Test 1: Check flake syntax +echo "Test 1: Checking flake syntax..." +if nix flake check --no-build; then + echo "✓ Flake syntax is valid" +else + echo "❌ Flake syntax check failed" + exit 1 +fi +echo "" + +# Test 2: Show flake outputs +echo "Test 2: Showing available outputs..." +nix flake show +echo "" + +# Test 3: Try building default package +echo "Test 3: Building default package..." +if nix build .#sqlit --print-build-logs; then + echo "✓ Default package builds successfully" +else + echo "❌ Default package build failed" + exit 1 +fi +echo "" + +# Test 4: Try building minimal package +echo "Test 4: Building minimal package..." +if nix build .#sqlit-minimal --print-build-logs; then + echo "✓ Minimal package builds successfully" +else + echo "❌ Minimal package build failed" + exit 1 +fi +echo "" + +# Test 5: Try building full package +echo "Test 5: Building full package..." +if nix build .#sqlit-full --print-build-logs; then + echo "✓ Full package builds successfully" +else + echo "❌ Full package build failed" + exit 1 +fi +echo "" + +# Test 6: Verify the default app works +echo "Test 6: Verifying default app..." +if nix run .#sqlit -- --version 2>&1 | head -5; then + echo "✓ Default app runs successfully" +else + echo "⚠️ App run test skipped (may require TTY or version flag may not exist)" + echo " This is not a critical failure - the package may still work correctly" +fi +echo "" + +echo "==========================================" +echo "All tests passed! ✓" +echo "==========================================" +echo "" +echo "Available packages:" +echo " - sqlit (default): Common dependencies" +echo " - sqlit-minimal: SQLite only" +echo " - sqlit-full: All available dependencies" +echo "" +echo "Try them with:" +echo " nix run .#sqlit" +echo " nix run .#sqlit-minimal" +echo " nix run .#sqlit-full"