-*- mode: org -*-
#+TITLE:       configuration nix
#+DESCRIPTION: makefile for spine
#+FILETAGS:    :sisu:build:tools:
#+AUTHOR:      Ralph Amissah
#+EMAIL:       [[mailto:ralph.amissah@gmail.com][ralph.amissah@gmail.com]]
#+COPYRIGHT:   Copyright (C) 2015 - 2025 Ralph Amissah
#+LANGUAGE:    en
#+STARTUP:     content hideblocks hidestars noindent entitiespretty
#+PROPERTY:    header-args  :exports code
#+PROPERTY:    header-args+ :noweb yes
#+PROPERTY:    header-args+ :results no
#+PROPERTY:    header-args+ :cache no
#+PROPERTY:    header-args+ :padline no
#+PROPERTY:    header-args+ :mkdirp yes
#+OPTIONS:     H:3 num:nil toc:t \n:t ::t |:t ^:nil -:t f:t *:t

* nix :nix:

#+NAME: nixpkgs_local
#+BEGIN_SRC sh
/srv/nix/nixpkgs
#+END_SRC

* sisu

- default.nix
- shell.nix

** flake.nix

#+HEADER: :tangle ../flake.nix
#+HEADER: :noweb yes
#+BEGIN_SRC nix
{
  description = "sisu parser & document generator";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; # "github:nixos/nixpkgs"; # "github:NixOS/nixpkgs/nixpkgs-unstable"; "nixpkgs/nixos-unstable"; "nixpkgs/nixos-21.11";
  outputs = {
    self,
    nixpkgs,
  } @ inputs: let
    pname = "sisu";
    version = "<<sisu_project_version>>";
    shell = ./shell.nix; # ./default.nix;
    devEnv = ./.envrc; # ./.envrc; # ./shell.nix; # ./default.nix;
    supportedSystems = [ "x86_64-linux" ]; # [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
    forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
    nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; }); # nixpkgs instantiated for supported system types
    checkPhase = ''
      runHook preCheck
      runHook postCheck
    '';
    preInstall = "";
    installPhase = ''
      runHook preInstall
      mkdir -p $out/lib
      mkdir -p $out/setup
      mkdir -p $out/data
      mkdir -p $out/bin
      cp -R ./lib/* $out/lib/.
      cp -R ./data/* $out/data/.
      cp -R ./setup/* $out/setup/.
      ln -s $out/setup/sisu_version.rb $out/version
      install -m755 ./bin/sisu $out/bin/sisu
      runHook postInstall
    '';
    postInstall = "";
  in {
    packages = forAllSystems (system: let
      pkgs = nixpkgsFor.${system};
    in
      with pkgs; {
        default = stdenv.mkDerivation {
          inherit pname;
          inherit version;
          meta.mainProgram = "sisu";
          executable = true;
          src = self;
          inherit shell;
          inherit devEnv;
          buildPhase = "";
          inherit checkPhase;
          inherit installPhase;
          inherit postInstall;
        };
        #vendorSha256 = "sha256-0Q00000000000000000000000000000000000000000=";
      });
    apps = forAllSystems (system: {
      default = {
        type = "app";
        program = "${self.packages.${system}.default}/bin/sisu";
      };
    });
    devShells = forAllSystems (system: let
      pkgs = nixpkgsFor.${system};
      shellHook = ''
        #${pkgs.nix}/bin/nix build ".#" --print-build-logs;
        nix build ".#" --print-build-logs;
        echo ""
        sisu -v
      '';
    in
      with pkgs; {
        default = mkShell {
          name = "sisu dev base shell";
          inherit shell;
          inherit devEnv;
          packages = [
            <<ruby_current>>
            sqlite
            jq
            git
          ];
          inherit shellHook;
        };
        dsh-epub = mkShell {
          name = "sisu dev shell for epub output";
          inherit shell;
          inherit devEnv;
          packages = [
            <<ruby_current>>
            sqlite
            graphicsmagick
            zip
            unzip
            xz
            libxml2
            html-tidy
            xmlstarlet
            epubcheck
            ebook_tools
            libxml2
            html-tidy
            xmlstarlet
            epubcheck
            ebook_tools
            epr
            sigil
            calibre #(suite includes: ebook-viewer)
            foliate
            validatePkgConfig
            jq
            #git
          ];
          inherit shellHook;
        };
        dsh-html = mkShell {
          name = "sisu dev shell for html output";
          inherit shell;
          inherit devEnv;
          packages = [
            <<ruby_current>>
            sqlite
            graphicsmagick
            zip
            unzip
            xz
            validatePkgConfig
            jq
            #git
          ];
          inherit shellHook;
        };
        dsh-latex-pdf = mkShell {
          name = "sisu dev shell for latex & pdf output";
          inherit shell;
          inherit devEnv;
          packages = [
            <<ruby_current>>
            sqlite
            graphicsmagick
            zip
            unzip
            xz
            source-sans-pro
            source-serif-pro
            source-code-pro
            texliveFull # texliveTeTeX
            noto-fonts
            noto-fonts-cjk-sans
            takao
            validatePkgConfig
            jq
            #git
          ];
          inherit shellHook;
        };
        dsh-sqlite = mkShell {
          name = "sisu dev shell for sqlite3 output";
          inherit shell;
          inherit devEnv;
          packages = [
            <<ruby_current>>
            sqlite
            graphicsmagick
            zip
            unzip
            xz
            validatePkgConfig
            jq
            #git
          ];
          inherit shellHook;
        };
        dsh-i18n = mkShell {
          name = "sisu dev shell internationalization, po4a";
          inherit shell;
          inherit devEnv;
          packages = [
            <<ruby_current>>
            sqlite
            graphicsmagick
            perl538Packages.Po4a
            validatePkgConfig
            jq
            #git
          ];
          inherit shellHook;
        };
        #default = import ./shell.nix {inherit pkgs;};
      });
  };
}
#+END_SRC

** shell.nix TODO

#+HEADER: :tangle ../shell.nix
#+HEADER: :shebang "#!/usr/bin/env -S nix-shell --impure\n#!nix-shell -i bash"
#+HEADER: :tangle-mode (identity #o755)
#+BEGIN_SRC nix
{pkgs ? import <nixpkgs> {}}:
with pkgs;
  mkShell {
    buildInputs = [
      <<ruby_current>>
      <<packages_project_relevant>>
      <<packages_build>>
      <<packages_extra>>
    ];
    shellHook = ''
    '';
  }
#+END_SRC
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
mkShell {
  buildInputs = [
    <<ruby_current>>
    sqlite
    graphicsmagick
    unzip
    xz
    zip
    openssl
    #texliveFull # texliveTeTeX
    nixFlakes
    validatePkgConfig
    nix-output-monitor
    nix-tree
    jq
    git
    #starship
  ];
  shellHook = ''
  '';
}
#+END_SRC

** packages.nix
*** ruby next

#+NAME: ruby_next
#+BEGIN_SRC nix
ruby<<ruby_version_next>>
rubyPackages<<ruby_version_next>>.rake
rubyPackages<<ruby_version_next>>.sqlite3
rubyPackages<<ruby_version_next>>.thor
#+END_SRC

*** ruby current

#+NAME: ruby_current
#+BEGIN_SRC nix
ruby<<ruby_version_current>>
rubyPackages<<ruby_version_current>>.rake
rubyPackages<<ruby_version_current>>.sqlite3
rubyPackages<<ruby_version_current>>.thor
#+END_SRC

*** ruby legacy

#+NAME: ruby_legacy
#+BEGIN_SRC nix
<<ruby_version_3_2>>
#+END_SRC

*** ruby versions current, next
**** ruby version next

#+NAME: ruby_version_next
#+BEGIN_SRC nix
_3_4
#+END_SRC

**** ruby version current

- default to current nix version, which is ruby 3.3 but not provided as a fixed lable/tag, so leave blank and will
  follow nix current nix

#+NAME: ruby_version_current
#+BEGIN_SRC nix
#+END_SRC

**** ruby version legacy

#+NAME: ruby_version_legacy
#+BEGIN_SRC nix
_3_2
#+END_SRC

*** ruby fixed versions
**** ruby 3.4 - ruby_version_3_4

#+NAME: ruby_version_3_4
#+BEGIN_SRC nix
ruby_3_4
rubyPackages_3_4.rake
rubyPackages_3_4.sqlite3
rubyPackages_3_4.thor
#+END_SRC

**** ruby 3.2 - ruby_version_3_2

#+NAME: ruby_version_3_2
#+BEGIN_SRC nix
ruby_3_2
rubyPackages_3_2.rake
rubyPackages_3_2.sqlite3
rubyPackages_3_2.thor
#+END_SRC

*** nix related packages

#+NAME: nix_packages
#+BEGIN_SRC nix
nix
bundler
bundix
#+END_SRC

*** project relevant packages

#+NAME: packages_project_relevant
#+BEGIN_SRC nix
sqlite
graphicsmagick
unzip
xz
zip
openssl
#texliveFull # texliveTeTeX
#+END_SRC

*** project misc build packages

#+NAME: packages_build
#+BEGIN_SRC nix
nixFlakes
validatePkgConfig
nix-output-monitor
nix-tree
jq
git
#+END_SRC

#+NAME: packages_extra
#+BEGIN_SRC nix
#starship
#+END_SRC

** sisu version SET VERSION :version:set:project:

#+NAME: sisu_project_version
#+BEGIN_SRC emacs-lisp
<<./sisu_version_info_and_doc_header_including_copyright_and_license.org:sisu_project_version()>>
#+END_SRC