Skip to content

Instantly share code, notes, and snippets.

@nonsequitur
Last active December 21, 2021 17:41
Show Gist options
  • Save nonsequitur/23893618120ee24d8c2f0e61c39dfd72 to your computer and use it in GitHub Desktop.
Save nonsequitur/23893618120ee24d8c2f0e61c39dfd72 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
outputPaths=$(nix-build --no-out-link - <<'EOF'
let
pkgs = (import <nixpkgs> {});
# The book source
src = let
second-edition = fetchBook {
rev = "6354bb99d3834303693658890426a7ec6d4c104e";
sha256 = "1w0p30d5p1yswfna1dw06ii5vajlij98b4x25bh7qzljb60hmivz";
};
develop = fetchBook {
rev = "bb8496fc9e841934c054c88a7b38525f234117ff";
sha256 = "029dczarqf3l1c0aqa16lfs3ff1q4i85wlnfzqrj45zw2zfiq2gs";
};
in
develop;
# The exact version of the build dependencies
pkgsSrc = fetchNixPkgs {
rev = "eafd703a63a598a33028aca6f1349544dfd09b65";
sha256 = "1kdd83ss8bcgvfn18ivvz4xy0sqc571bspnp1whfdf5lz4yc3zdc";
};
buildBook = { asciidoc-full-with-plugins, runCommand }:
let
# Modify as needed
formatsToBuild = with buildScripts; [
htmlWithImgsDir
standaloneHtml
epub
pdf
];
buildScripts = {
htmlWithImgsDir = ''
dest=$out/mastering_bitcoin_htm
mkdir $dest
asciidoc -a toc -o $dest/mastering_bitcoin.htm -d book book.asciidoc
cp -r $src/images $dest
'';
standaloneHtml = "asciidoc -a data-uri -a icons -a toc -a max-width=55em -o $out/mastering_bitcoin_standalone.htm -d book book.asciidoc";
epub = ''
a2x -f epub -d book --no-xmllint --destination-dir=$out $src/book.asciidoc
mv $out/book.epub $out/mastering_bitcoin.epub
'';
pdf =
# When building pdfs, a2x can only write its output to the source directory.
# To work around this, copy src, build and delete src.
''
cp -r $src/ $TMP/src
cd $TMP/src
chmod '+w' .
a2x -f pdf --fop -d book -L book.asciidoc
mv book.pdf $out/mastering_bitcoin.pdf
'';
};
buildBook = buildScript:
runCommand "mastering-bitcoin" {
inherit src;
buildInputs = [ asciidoc-full-with-plugins ];
}
(''
mkdir $out
cd $src
'' + buildScript);
bookOutputs = runCommand "mastering-bitcoin-outputs" {
outputPaths = map buildBook formatsToBuild;
} ''
for x in $outputPaths; do
echo $x/* >> $out
done
'';
in
bookOutputs;
fetchBook = { rev, sha256 }:
pkgs.fetchFromGitHub {
inherit sha256 rev;
owner = "bitcoinbook";
repo = "bitcoinbook";
name = "mastering-bitcoin-${builtins.substring 0 6 rev}";
};
fetchNixPkgs = { rev, sha256 }:
pkgs.fetchFromGitHub {
inherit sha256 rev;
owner = "NixOS";
repo = "nixpkgs-channels";
name = "nixpkgs-${builtins.substring 0 6 rev}";
};
in
(import pkgsSrc {}).callPackage buildBook {}
EOF
) && printf "\n\n" && echo "Output paths:" && cat $outputPaths
@kostaz
Copy link

kostaz commented Nov 18, 2017

Hey, I tried it on Ubuntu 16.04 64-bit just a moment before...

➜  /tmp 
./build-mastering-bitcoin.sh  
error: attribute ‘fetchNixPkgs’ missing, at (string):14:14

Can you take a look?

@nonsequitur
Copy link
Author

It's fixed.

@EduardJT
Copy link

Hello! How can I run this on Mac terminal? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment