deploying a nanoc site to ipfs

This site is generated using nanoc my go to tool for site generation since some years. It is ruby base which makes everything easy - obviously… Anyways instead of having this script that publishes to ipfs I decided to try to hook into the deploy thing in nanoc. The result was going from two lines of shell to 26 lines of Ruby. A bit sad but that is how it is. Mostly of it boilerplate. I had a look into other deployers inside the nanoc source code and came up with this beauty.

require 'ipfs-api'

module Nanoc
  module Deploying
    module Deployers
      class Ipfs < ::Nanoc::Deploying::Deployer
        identifier :ipfs

        def run
          ipfs = IPFS::Connection.new
          key = config[:key]
          ipfs.add Dir.new('output') do |node|
            if node.finished?
              unless node.parent
                puts "publishing #{node.name} to ipns under the key #{key}"
                ipns_hash = ipfs.name.publish(node, key)
                puts "published to ipns hash #{ipns_hash}"
              end
            end
          end
        end
      end
    end
  end
end

As often is the case in code like this there is an end party towards the end. Maybe the saddest feature of Ruby to be honest. As you can see I am using a library called ipfs-api to make calls to the running IPFS daemon. These are made over an API rather than using the command line. (Most likely the command line does the same wrapping.)

I put this file in the lib folder and to make it exist when running deploy I also had to add the following to the nanoc.yml configuration file.

deploy:
  default:
    kind: ipfs
    key: mazin

Where the key had been previously generated as outlined in the IPFS publishing article. The kind in the yaml file needs to match the identifier in the Ipfs class. A classic string matching opportunity….

All in all a worthy task.

written by fredrik at 2023-04-11

More content on ipfs

More content on nanoc