Remove GRUB from MacOS boot partition

November 6, 2020

I’ve got Linux on an external SSD drive which I can boot from my Macbook Pro using Command-D while powering up to select Linux as the boot drive.

The only glitch is that when installing Linux, by mistake I installed GRUB (Linux boot loader) on my Mac root partition. The easiest way to fix this is to boot the machine into repair mode using Command-R and reinstall MacOS. This won’t erase your data or applications, just reinstall the operating system and rewrite the boot partition. (The process takes about an hour, so plan ahead.)

How to use Calibre with DeDRM tools on Linux to backup your Kindle library

September 19, 2020

Amazon Kindle’s ecosystem lets you buy an ebook and begin reading seconds after purchasing it. However, as most of Amazon’s book are DRM-protected, you cannot make a backup of your library or convert your books´ format to read them on another platform. Companies come and go, and even if Amazon’s is here for the long run, it’s policies regarding how you can access the content your paid for may change in the future.

Using a Kindle app version prior to 1.26 On Windows or MacOs, you can use Calibre with Aprentice Alf’s DeDRM plugin to convert the books from your Kindle app on your computer to a DRM-free format. The plugin automatically extracts the necessary key the Kindle app uses to encrypt your books and makes the process transparent.

On Linux, you can use Wine to install the Kindle app. However, the DeDRM plugin doesn’t extract the decryption key automatically. To extract the key, you need to install Python and PyCrypto in Wine and run kindlekey.py manually. Once you have the key, you can import it into Calibre using the DeDRM plugin.

I’ve tested the setup using Ubuntu 20.04.

  1. Install Calibre for Linux.

  2. Download Aprentice Alf’s DeDRM tools. Uncompress the file, open Calibre and install the DeDRM plugin.

  3. Uncompress the DeDRM plugin file (DeDRM_Plugin.zip) and verify that there’s a file named kindlekey.py.

  4. Install wine:

$ sudo dpkg --add-architecture i386
$ sudo apt update
$ sudo apt install wine64 wine32
(...)
$ wine --version
wine-5.0 (Ubuntu 5.0-3ubuntu1)
  1. Search on Google for the Kindle app for Windows. You need version 1.26 or earlier. Install it using wine:
$ wine kindleforpc-installer-1.15.43061.exe

Run the Kindle app and register it with your Amazon account. You should be able to access your Kindle library without problems.

  1. Download the most recent version of Python 2.7 for Windows from Python.org. Install it using wine:
$ wine msiexec /i python-2.7.18.msi
  1. Download a Windows installer for PyCrypto for Python 2.7. Install it using wine. (If you installed a 32-bit version of Python, use a 32-bit version of Pycrypto.)
$ wine pycrypto-2.6.win32-py2.7.exe
  1. In the Linux terminal, naviagate to the folder where your kindlekey.py file resides. Then open a Windows shell using wine, and run kindlekey.py:
~/Downloads/DeDRM/original/DeDRM_tools_6.8.0/DeDRM_Plugin$ wine cmd.exe
Microsoft Windows 6.1.7601

D:(...) \DeDRM_Plugin>python kindlekey.py
Using Library AlfCrypto DLL/DYLIB/SO
searching for kinfoFiles in C:\users\(...)\Local Settings\Application Data
Found K4PC 1.9+ kinf2011 file: C:\users\(...)\Local Settings\Application Data\Amazon\Kindle\storage\.kinf2011
Decrypted key file using IDString '0' and UserName 'xxxxxxxxxx'

D:(...) \DeDRM_Plugin> exit
  1. You should now have a file named kindlekey1.k4i in your directory. Open Calibre, click on the Preferences button. Navigate to Plugins/File Type. Locate the DeDRM plugin, and click the ‘Customize Plugin’ button and then click on Kindle for Mac/PC ebooks. Import the key into the DeDRM plugin and restart Calibre.

  2. Locate your books inside the wine installation. Wine stores the Windows installation under ~/.wine. The Kindle books are usually located in ~/.wine/drive_c/users/[your username]/My Documents/My Kindle Content.` I suggest you create a link to your Linux home directory.

You can now import books from your Kindle directory into Calibre, and make a DRM-free backup copy.

Disclaimer: This instructions are intended for making a backup copy of your Kindle library, my intention is not to encourage piracy in any way.

Is it Worth the Time?

September 1, 2020

I’ll copy this xkcd comic here so I can find when I fall into another Emacs org-mode time sink.

Is it worth the time?

Source: https://xkcd.com/1205/

Sanderson’s Laws of Magic

August 30, 2020

Source: https://www.brandonsanderson.com/sandersons-first-law/

Sanderson’s First Law of Magic

An author’s ability to solve conflict with magic is DIRECTLY PROPORTIONAL to how well the reader understands said magic.

Books with Soft Magic systems are books where magic is included in order to establish a sense of wonder and give the setting a fantastical feel. Really good writers of soft magic systems very rarely use their magic to solve problems in their books.

So, the rule for soft magic systems is to use the magic for ambiance but never for plot.

With Hard Magic, the author explicitly describes the rules of magic. More, the magic itself is a character. By showing off its laws and rules, the author provides twists, worldbuilding, and characterization.

In this context, you can use magic to solve problems because the reader understands how magic works.

Sanderson’s Second Law of Magic

Limitations > Powers

What’s important to what heroes are is not what they can do, but what they have trouble doing. When designing a magic system, it’s very difficult to come up with a magical power that’s truly original. However, you can be original with the limitation. Example: in The Wheel of Time fantasy series, practitioners of magic draw forth “threads” of the Aristotelian elements –fire, earth, water, air, and spirit– and then “weave” the different powers into complex patterns, which then accomplish a goal. The limitation of the magic is that its practitioners must use skill and knowledge, and take time to create what they’re making.

Limitations on a magic system do several things:

  • Struggle, because it forces the characters to work for their goals. They have to be more clever to overcome their problems. They need to work harder. All this leads to more interesting scenes. The nature of the writing encourages better writing.
  • An excellent magic system will increase tension. Superman fighting an enemy with kryptonite is far more tense than Superman just fighting an enemy.
  • Limitations force you, as a writer, to create more depth to your world and characters. In this sense, even outside the fantasy genre limitations are more important than abilities.

Different kinds of limiting factors of magic systems

  • Limitations. There are things that, for one reason or another, the magic system simply cannot do. Every magic has basic limitations, defined as the limited scope of the power. (i.e., Superman can’t see through lead.)

Sanderson suggests that limitations be more encompassing than simple parameters.

  • Weaknesses are things that, rather than being things the power cannot do, enemies can exploit. In contrast to a limitation, a weakness lives your caracter vulnerable. (Again, the obvious example is kryptonite.)
  • Costs. Using magic or being associated with it has a cost. Costs can be abstract (in the Wheel of Times, men who weave magic end up going crazy), or concrete (you need a certain quantity of mana or something to make certain spell work).
  • Others. These definitions are about how you approach certain aspects of your magic system, and how you don’t allow yourself to take easy answers.
    • How does one gain access to magic? Stardard ways: innate magic and learned magic.
    • How is the magic powered? Mitigate the breaking of physics law by preserving of the laws of thermodynamics. Energy to power the magic has to come from somewhere.
    • How often can the magic be used? Do you need special implements (wands…) or a special state of mind?

The point is not to write more complex plots but to tell better stories. What your characters have trouble accomplishing in a plot is going to be far more interesting than what they can do easily.

Sanderson’s Third Law of Magic

Expand what you already have before you add something new.

This law challenges the writer to create deep worldbuilding instead of just wide worldbuilding. Even if one can enjoy stories with massive worldbuilding, intrincate worlds, and clever use of magic, you can make the story boring by overburdening it with too much expository worldbuilding. Often great storytelling happens when a thoughtful writer changes one or two things about what we know, then extrapolates purposefully through all of the ramifications of that change. Variants of this: one truly evil villain usually works better than lots of mediocre villains.

Three useful directions where you can expand magic

  • Extrapolate. Extrapolation is about asking “what happens when” questions. How do the changes you’ve made affect the world as a whole? The changes should be aligned with the story goal’s and length. Epic fantasy may touch history and economics. An urban fantasy may better focus just on one specific factor.
  • Interconnect. Sometimes this law can go too far. Having large list of powers can help create a more unique experience for your storytelling.

Try tying your powers, cultures and themes together. Tie them thematically, and ask yourself how they play into the themes of your novel. This will help you worldbuild and expand, instead of adding.

  • Streamline. Look over your cultures, magics, and even characters and ask yourself, “Where can I combine these?”

Conclusion: expand, don’t add.

Migrating from Pelican to Hugo

August 16, 2020

I’ve migrated this blog from Pelican to Hugo.

This blog has always been a static/baked blog. Content is just a collection of html files which reside in a Linode instance. This allows me to use a very minimal server instance to host it. Dynamic content generators like Wordpress generate content by extracting the articles from a database and producing a webpage on the fly, they allow for more complex setups, but also have a higher footprint.

The problem with Pelican is that once in a while, when I upgraded the software, my site configuration file broke. So instead of just publishing a new post, I had to spend time figuring out what had changed, testing the new configuration, etc. After evaluating some alternatives, I decided to give Hugo a try. It took me one morning to figure out Hugo, have a test site running on my laptop, move my posts to Hugo, and republish the blog on my server.

For the actual migration, I wrote a script in Python called pelican-to-hugo that collects all the Markdown files from my blog’s Pelican directory, and converts each post’s header to Hugo’s front matter in TOML format. You can modify the script to suit your needs.

Some issues with the migration:

  • I broke my urls in the migration. I previously used dates as part of the post’s path (i.e., https://code.zoia.org/2020/08/migrating-from-pelican-to-hugo/), now I’m simply using `/posts/migrating-from-pelican-to-hugo/**. I could write some code for the webserver to handle the redirection…

    Update 2020-08-23: Fixed the posts urls by duplicating the content under the original file structure.

  • I fixed my internal image links by hand: searching in the post’s directory for images, and changing the path. I did this by hand instead of writting code to do it because I don’t use that many images in my posts.

For know, I’m using a nice theme from Djordje Atlialp while I figure out how templates work in Hugo.